restructure code into 3 subpackages for backend frontend and shared
This commit is contained in:
parent
0acc5bb1e3
commit
887600f964
|
@ -3,8 +3,7 @@ package backend
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/pboehm/ddns/config"
|
||||
"github.com/pboehm/ddns/hosts"
|
||||
"github.com/pboehm/ddns/shared"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -26,11 +25,11 @@ type Response struct {
|
|||
}
|
||||
|
||||
type HostLookup struct {
|
||||
config *config.Config
|
||||
hosts hosts.HostBackend
|
||||
config *shared.Config
|
||||
hosts shared.HostBackend
|
||||
}
|
||||
|
||||
func NewHostLookup(config *config.Config, hostsBackend hosts.HostBackend) *HostLookup {
|
||||
func NewHostLookup(config *shared.Config, hostsBackend shared.HostBackend) *HostLookup {
|
||||
return &HostLookup{config, hostsBackend}
|
||||
}
|
||||
|
||||
|
@ -52,7 +51,7 @@ func (l *HostLookup) Lookup(request *Request) (*Response, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var host *hosts.Host
|
||||
var host *shared.Host
|
||||
if host, err = l.hosts.GetHost(hostname); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -2,17 +2,16 @@ package backend
|
|||
|
||||
import (
|
||||
"errors"
|
||||
c "github.com/pboehm/ddns/config"
|
||||
h "github.com/pboehm/ddns/hosts"
|
||||
"github.com/pboehm/ddns/shared"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testHostBackend struct {
|
||||
hosts map[string]*h.Host
|
||||
hosts map[string]*shared.Host
|
||||
}
|
||||
|
||||
func (b *testHostBackend) GetHost(hostname string) (*h.Host, error) {
|
||||
func (b *testHostBackend) GetHost(hostname string) (*shared.Host, error) {
|
||||
host, ok := b.hosts[hostname]
|
||||
if ok {
|
||||
return host, nil
|
||||
|
@ -21,20 +20,20 @@ func (b *testHostBackend) GetHost(hostname string) (*h.Host, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (b *testHostBackend) SetHost(host *h.Host) error {
|
||||
func (b *testHostBackend) SetHost(host *shared.Host) error {
|
||||
b.hosts[host.Hostname] = host
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildLookup(domain string) (*c.Config, *testHostBackend, *HostLookup) {
|
||||
config := &c.Config{
|
||||
func buildLookup(domain string) (*shared.Config, *testHostBackend, *HostLookup) {
|
||||
config := &shared.Config{
|
||||
Verbose: false,
|
||||
Domain: domain,
|
||||
SOAFqdn: "dns" + domain,
|
||||
}
|
||||
|
||||
hosts := &testHostBackend{
|
||||
hosts: map[string]*h.Host{
|
||||
hosts: map[string]*shared.Host{
|
||||
"www": {
|
||||
Hostname: "www",
|
||||
Ip: "10.11.12.13",
|
||||
|
|
11
ddns.go
11
ddns.go
|
@ -3,14 +3,13 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"github.com/pboehm/ddns/backend"
|
||||
"github.com/pboehm/ddns/config"
|
||||
"github.com/pboehm/ddns/hosts"
|
||||
"github.com/pboehm/ddns/web"
|
||||
"github.com/pboehm/ddns/frontend"
|
||||
"github.com/pboehm/ddns/shared"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var serviceConfig *config.Config = &config.Config{}
|
||||
var serviceConfig *shared.Config = &shared.Config{}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&serviceConfig.Domain, "domain", "",
|
||||
|
@ -49,10 +48,10 @@ func main() {
|
|||
flag.Parse()
|
||||
validateCommandArgs()
|
||||
|
||||
redis := hosts.NewRedisBackend(serviceConfig)
|
||||
redis := shared.NewRedisBackend(serviceConfig)
|
||||
defer redis.Close()
|
||||
|
||||
lookup := backend.NewHostLookup(serviceConfig, redis)
|
||||
|
||||
web.NewWebService(serviceConfig, redis, lookup).Run()
|
||||
frontend.NewWebService(serviceConfig, redis, lookup).Run()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package web
|
||||
package frontend
|
||||
|
||||
const indexTemplate string = `
|
||||
<!DOCTYPE html>
|
|
@ -1,10 +1,9 @@
|
|||
package web
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pboehm/ddns/backend"
|
||||
"github.com/pboehm/ddns/config"
|
||||
"github.com/pboehm/ddns/hosts"
|
||||
"github.com/pboehm/ddns/shared"
|
||||
"gopkg.in/gin-gonic/gin.v1"
|
||||
"html/template"
|
||||
"log"
|
||||
|
@ -15,12 +14,12 @@ import (
|
|||
)
|
||||
|
||||
type WebService struct {
|
||||
config *config.Config
|
||||
hosts hosts.HostBackend
|
||||
config *shared.Config
|
||||
hosts shared.HostBackend
|
||||
lookup *backend.HostLookup
|
||||
}
|
||||
|
||||
func NewWebService(config *config.Config, hosts hosts.HostBackend, lookup *backend.HostLookup) *WebService {
|
||||
func NewWebService(config *shared.Config, hosts shared.HostBackend, lookup *backend.HostLookup) *WebService {
|
||||
return &WebService{
|
||||
config: config,
|
||||
hosts: hosts,
|
||||
|
@ -66,7 +65,7 @@ func (w *WebService) Run() {
|
|||
return
|
||||
}
|
||||
|
||||
host := &hosts.Host{Hostname: hostname, Ip: "127.0.0.1"}
|
||||
host := &shared.Host{Hostname: hostname, Ip: "127.0.0.1"}
|
||||
host.GenerateAndSetToken()
|
||||
|
||||
if err = w.hosts.SetHost(host); err != nil {
|
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package shared
|
||||
|
||||
type Config struct {
|
||||
Verbose bool
|
|
@ -1,4 +1,4 @@
|
|||
package hosts
|
||||
package shared
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
|
@ -1,9 +1,8 @@
|
|||
package hosts
|
||||
package shared
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"github.com/pboehm/ddns/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -12,7 +11,7 @@ type RedisBackend struct {
|
|||
pool *redis.Pool
|
||||
}
|
||||
|
||||
func NewRedisBackend(config *config.Config) *RedisBackend {
|
||||
func NewRedisBackend(config *Config) *RedisBackend {
|
||||
return &RedisBackend{
|
||||
expirationSeconds: config.HostExpirationDays * 24 * 60 * 60,
|
||||
pool: &redis.Pool{
|
Loading…
Reference in New Issue