move config parsing into the shared package

This commit is contained in:
Philipp Böhm 2017-12-03 23:18:47 +01:00
parent b7c07b86a0
commit ce032237a8
4 changed files with 50 additions and 41 deletions

View File

@ -41,5 +41,5 @@ func (b *Backend) Run() error {
} }
}) })
return r.Run(b.config.BackendListen) return r.Run(b.config.ListenBackend)
} }

39
ddns.go
View File

@ -1,56 +1,21 @@
package main package main
import ( import (
"flag"
"github.com/pboehm/ddns/backend" "github.com/pboehm/ddns/backend"
"github.com/pboehm/ddns/frontend" "github.com/pboehm/ddns/frontend"
"github.com/pboehm/ddns/shared" "github.com/pboehm/ddns/shared"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"log" "log"
"strings"
) )
var serviceConfig *shared.Config = &shared.Config{} var serviceConfig *shared.Config = &shared.Config{}
func init() { func init() {
flag.StringVar(&serviceConfig.Domain, "domain", "", serviceConfig.Initialize()
"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")
}
} }
func main() { func main() {
flag.Parse() serviceConfig.Validate()
validateCommandArgs()
redis := shared.NewRedisBackend(serviceConfig) redis := shared.NewRedisBackend(serviceConfig)
defer redis.Close() defer redis.Close()

View File

@ -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 // Get the Remote Address of the client. At First we try to get the

View File

@ -1,11 +1,55 @@
package shared package shared
import (
"flag"
"log"
"strings"
)
type Config struct { type Config struct {
Verbose bool Verbose bool
Domain string Domain string
SOAFqdn string SOAFqdn string
HostExpirationDays int HostExpirationDays int
FrontendListen string ListenFrontend string
BackendListen string ListenBackend string
RedisHost 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")
}
}