From 887600f964510c2a598315d396a43e92b2cd5a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Sun, 3 Dec 2017 22:42:25 +0100 Subject: [PATCH] restructure code into 3 subpackages for backend frontend and shared --- backend/lookup.go | 11 +++++------ backend/lookup_test.go | 15 +++++++-------- ddns.go | 11 +++++------ {web => frontend}/template.go | 2 +- {web => frontend}/web.go | 13 ++++++------- {config => shared}/config.go | 2 +- {hosts => shared}/hosts.go | 2 +- {hosts => shared}/redis.go | 5 ++--- 8 files changed, 28 insertions(+), 33 deletions(-) rename {web => frontend}/template.go (99%) rename {web => frontend}/web.go (92%) rename {config => shared}/config.go (92%) rename {hosts => shared}/hosts.go (97%) rename {hosts => shared}/redis.go (92%) diff --git a/backend/lookup.go b/backend/lookup.go index 10c5903..8c3dfe8 100644 --- a/backend/lookup.go +++ b/backend/lookup.go @@ -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 } diff --git a/backend/lookup_test.go b/backend/lookup_test.go index 0ec7ed7..7fb52ce 100644 --- a/backend/lookup_test.go +++ b/backend/lookup_test.go @@ -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", diff --git a/ddns.go b/ddns.go index 10f0abc..8d5969c 100644 --- a/ddns.go +++ b/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() } diff --git a/web/template.go b/frontend/template.go similarity index 99% rename from web/template.go rename to frontend/template.go index 94424a9..8c38d57 100644 --- a/web/template.go +++ b/frontend/template.go @@ -1,4 +1,4 @@ -package web +package frontend const indexTemplate string = ` diff --git a/web/web.go b/frontend/web.go similarity index 92% rename from web/web.go rename to frontend/web.go index f1f6ff7..65c9303 100644 --- a/web/web.go +++ b/frontend/web.go @@ -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 { diff --git a/config/config.go b/shared/config.go similarity index 92% rename from config/config.go rename to shared/config.go index 2532421..664829a 100644 --- a/config/config.go +++ b/shared/config.go @@ -1,4 +1,4 @@ -package config +package shared type Config struct { Verbose bool diff --git a/hosts/hosts.go b/shared/hosts.go similarity index 97% rename from hosts/hosts.go rename to shared/hosts.go index 6d55d91..bfc3bf3 100644 --- a/hosts/hosts.go +++ b/shared/hosts.go @@ -1,4 +1,4 @@ -package hosts +package shared import ( "crypto/sha1" diff --git a/hosts/redis.go b/shared/redis.go similarity index 92% rename from hosts/redis.go rename to shared/redis.go index 89d91ab..22badd6 100644 --- a/hosts/redis.go +++ b/shared/redis.go @@ -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{