diff --git a/backend/backend.go b/backend/backend.go index dbf9f73..65c6e10 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -41,5 +41,5 @@ func (b *Backend) Run() error { } }) - return r.Run(b.config.BackendListen) + return r.Run(b.config.ListenBackend) } diff --git a/ddns.go b/ddns.go index 15983aa..ea125a8 100644 --- a/ddns.go +++ b/ddns.go @@ -1,56 +1,21 @@ package main import ( - "flag" "github.com/pboehm/ddns/backend" "github.com/pboehm/ddns/frontend" "github.com/pboehm/ddns/shared" "golang.org/x/sync/errgroup" "log" - "strings" ) var serviceConfig *shared.Config = &shared.Config{} func init() { - flag.StringVar(&serviceConfig.Domain, "domain", "", - "The subdomain which should be handled by DDNS") - - flag.StringVar(&serviceConfig.SOAFqdn, "soa_fqdn", "", - "The FQDN of the DNS server which is returned as a SOA record") - - flag.StringVar(&serviceConfig.BackendListen, "listen-backend", ":8057", - "Which socket should the backend web service use to bind itself") - - flag.StringVar(&serviceConfig.FrontendListen, "listen-frontend", ":8080", - "Which socket should the frontend web service use to bind itself") - - flag.StringVar(&serviceConfig.RedisHost, "redis", ":6379", - "The Redis socket that should be used") - - flag.IntVar(&serviceConfig.HostExpirationDays, "expiration-days", 10, - "The number of days after a host is released when it is not updated") - - flag.BoolVar(&serviceConfig.Verbose, "verbose", false, - "Be more verbose") -} - -func validateCommandArgs() { - if serviceConfig.Domain == "" { - log.Fatal("You have to supply the domain via --domain=DOMAIN") - } else if !strings.HasPrefix(serviceConfig.Domain, ".") { - // get the domain in the right format - serviceConfig.Domain = "." + serviceConfig.Domain - } - - if serviceConfig.SOAFqdn == "" { - log.Fatal("You have to supply the server FQDN via --soa_fqdn=FQDN") - } + serviceConfig.Initialize() } func main() { - flag.Parse() - validateCommandArgs() + serviceConfig.Validate() redis := shared.NewRedisBackend(serviceConfig) defer redis.Close() diff --git a/frontend/frontend.go b/frontend/frontend.go index 976948e..d868e57 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -121,7 +121,7 @@ func (f *Frontend) Run() error { }) }) - return r.Run(f.config.FrontendListen) + return r.Run(f.config.ListenFrontend) } // Get the Remote Address of the client. At First we try to get the diff --git a/shared/config.go b/shared/config.go index 42326d6..72debf1 100644 --- a/shared/config.go +++ b/shared/config.go @@ -1,11 +1,55 @@ package shared +import ( + "flag" + "log" + "strings" +) + type Config struct { Verbose bool Domain string SOAFqdn string HostExpirationDays int - FrontendListen string - BackendListen string + ListenFrontend string + ListenBackend string RedisHost string } + +func (c *Config) Initialize() { + flag.StringVar(&c.Domain, "domain", "", + "The subdomain which should be handled by DDNS") + + flag.StringVar(&c.SOAFqdn, "soa_fqdn", "", + "The FQDN of the DNS server which is returned as a SOA record") + + flag.StringVar(&c.ListenBackend, "listen-backend", ":8057", + "Which socket should the backend web service use to bind itself") + + flag.StringVar(&c.ListenFrontend, "listen-frontend", ":8080", + "Which socket should the frontend web service use to bind itself") + + flag.StringVar(&c.RedisHost, "redis", ":6379", + "The Redis socket that should be used") + + flag.IntVar(&c.HostExpirationDays, "expiration-days", 10, + "The number of days after a host is released when it is not updated") + + flag.BoolVar(&c.Verbose, "verbose", false, + "Be more verbose") +} + +func (c *Config) Validate() { + flag.Parse() + + if c.Domain == "" { + log.Fatal("You have to supply the domain via --domain=DOMAIN") + } else if !strings.HasPrefix(c.Domain, ".") { + // get the domain in the right format + c.Domain = "." + c.Domain + } + + if c.SOAFqdn == "" { + log.Fatal("You have to supply the server FQDN via --soa_fqdn=FQDN") + } +}