Skip to content

Commit ce03223

Browse files
committed
move config parsing into the shared package
1 parent b7c07b8 commit ce03223

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

backend/backend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ func (b *Backend) Run() error {
4141
}
4242
})
4343

44-
return r.Run(b.config.BackendListen)
44+
return r.Run(b.config.ListenBackend)
4545
}

ddns.go

+2-37
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,21 @@
11
package main
22

33
import (
4-
"flag"
54
"github.com/pboehm/ddns/backend"
65
"github.com/pboehm/ddns/frontend"
76
"github.com/pboehm/ddns/shared"
87
"golang.org/x/sync/errgroup"
98
"log"
10-
"strings"
119
)
1210

1311
var serviceConfig *shared.Config = &shared.Config{}
1412

1513
func init() {
16-
flag.StringVar(&serviceConfig.Domain, "domain", "",
17-
"The subdomain which should be handled by DDNS")
18-
19-
flag.StringVar(&serviceConfig.SOAFqdn, "soa_fqdn", "",
20-
"The FQDN of the DNS server which is returned as a SOA record")
21-
22-
flag.StringVar(&serviceConfig.BackendListen, "listen-backend", ":8057",
23-
"Which socket should the backend web service use to bind itself")
24-
25-
flag.StringVar(&serviceConfig.FrontendListen, "listen-frontend", ":8080",
26-
"Which socket should the frontend web service use to bind itself")
27-
28-
flag.StringVar(&serviceConfig.RedisHost, "redis", ":6379",
29-
"The Redis socket that should be used")
30-
31-
flag.IntVar(&serviceConfig.HostExpirationDays, "expiration-days", 10,
32-
"The number of days after a host is released when it is not updated")
33-
34-
flag.BoolVar(&serviceConfig.Verbose, "verbose", false,
35-
"Be more verbose")
36-
}
37-
38-
func validateCommandArgs() {
39-
if serviceConfig.Domain == "" {
40-
log.Fatal("You have to supply the domain via --domain=DOMAIN")
41-
} else if !strings.HasPrefix(serviceConfig.Domain, ".") {
42-
// get the domain in the right format
43-
serviceConfig.Domain = "." + serviceConfig.Domain
44-
}
45-
46-
if serviceConfig.SOAFqdn == "" {
47-
log.Fatal("You have to supply the server FQDN via --soa_fqdn=FQDN")
48-
}
14+
serviceConfig.Initialize()
4915
}
5016

5117
func main() {
52-
flag.Parse()
53-
validateCommandArgs()
18+
serviceConfig.Validate()
5419

5520
redis := shared.NewRedisBackend(serviceConfig)
5621
defer redis.Close()

frontend/frontend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (f *Frontend) Run() error {
121121
})
122122
})
123123

124-
return r.Run(f.config.FrontendListen)
124+
return r.Run(f.config.ListenFrontend)
125125
}
126126

127127
// Get the Remote Address of the client. At First we try to get the

shared/config.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
11
package shared
22

3+
import (
4+
"flag"
5+
"log"
6+
"strings"
7+
)
8+
39
type Config struct {
410
Verbose bool
511
Domain string
612
SOAFqdn string
713
HostExpirationDays int
8-
FrontendListen string
9-
BackendListen string
14+
ListenFrontend string
15+
ListenBackend string
1016
RedisHost string
1117
}
18+
19+
func (c *Config) Initialize() {
20+
flag.StringVar(&c.Domain, "domain", "",
21+
"The subdomain which should be handled by DDNS")
22+
23+
flag.StringVar(&c.SOAFqdn, "soa_fqdn", "",
24+
"The FQDN of the DNS server which is returned as a SOA record")
25+
26+
flag.StringVar(&c.ListenBackend, "listen-backend", ":8057",
27+
"Which socket should the backend web service use to bind itself")
28+
29+
flag.StringVar(&c.ListenFrontend, "listen-frontend", ":8080",
30+
"Which socket should the frontend web service use to bind itself")
31+
32+
flag.StringVar(&c.RedisHost, "redis", ":6379",
33+
"The Redis socket that should be used")
34+
35+
flag.IntVar(&c.HostExpirationDays, "expiration-days", 10,
36+
"The number of days after a host is released when it is not updated")
37+
38+
flag.BoolVar(&c.Verbose, "verbose", false,
39+
"Be more verbose")
40+
}
41+
42+
func (c *Config) Validate() {
43+
flag.Parse()
44+
45+
if c.Domain == "" {
46+
log.Fatal("You have to supply the domain via --domain=DOMAIN")
47+
} else if !strings.HasPrefix(c.Domain, ".") {
48+
// get the domain in the right format
49+
c.Domain = "." + c.Domain
50+
}
51+
52+
if c.SOAFqdn == "" {
53+
log.Fatal("You have to supply the server FQDN via --soa_fqdn=FQDN")
54+
}
55+
}

0 commit comments

Comments
 (0)