From 7b93ff425df053daa0a2d9dfb1b3ec89e32b52a1 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Sun, 22 Aug 2021 14:46:48 +0200 Subject: [PATCH] - Added sample default config (needs manual updating) - Reworked service and webapi config to Viper unmarshalling - Added Pretty Printers to config --- default-config.yml | 8 ++++++++ service/config.go | 33 ++++++++++++++++++++++++++------- service/dns.go | 6 +++--- service/userconfig.go | 2 +- webapi/config.go | 13 ++++++++++++- webapi/server.go | 4 ++++ 6 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 default-config.yml diff --git a/default-config.yml b/default-config.yml new file mode 100644 index 0000000..95e1501 --- /dev/null +++ b/default-config.yml @@ -0,0 +1,8 @@ +service: + dns: + defaultttl: 60 + server: 10.10.11.254:53 + users: + configdir: users/ +webapi: + listenaddress: :8080 diff --git a/service/config.go b/service/config.go index 4cef7b6..077b511 100644 --- a/service/config.go +++ b/service/config.go @@ -1,11 +1,24 @@ package service -import "github.com/spf13/viper" +import ( + "encoding/json" + "log" + + "github.com/spf13/viper" +) type config struct { - DNSServer string - DNSDefaultTTL uint32 - UsersConfigDir string + DNS configDNS + Users configUsers +} + +type configDNS struct { + Server string + DefaultTTL uint32 +} + +type configUsers struct { + ConfigDir string } var C config @@ -21,7 +34,13 @@ func SetConfigDefaults() { } func LoadConfig() { - C.DNSServer = viper.GetString("Service.DNS.Server") - C.DNSDefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL") - C.UsersConfigDir = viper.GetString("Service.Users.ConfigDir") + viper.UnmarshalKey("Service", &C) +} + +func (obj *config) PrettyPrint() string { + s, err := json.MarshalIndent(obj, "", " ") + if err != nil { + log.Fatalf("Failed to pretty print Service Config via JSON: %v", err) + } + return string(s) } diff --git a/service/dns.go b/service/dns.go index 1d9035f..6efade9 100644 --- a/service/dns.go +++ b/service/dns.go @@ -42,7 +42,7 @@ func UpdateDNSEntry(domain string, hostname string, ip4 net.IP, ip6 net.IP) erro Name: fqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, - Ttl: C.DNSDefaultTTL, + Ttl: C.DNS.DefaultTTL, }, A: ip4bin, }) @@ -58,7 +58,7 @@ func UpdateDNSEntry(domain string, hostname string, ip4 net.IP, ip6 net.IP) erro Name: fqdn, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, - Ttl: C.DNSDefaultTTL, + Ttl: C.DNS.DefaultTTL, }, AAAA: ip6bin, }) @@ -68,7 +68,7 @@ func UpdateDNSEntry(domain string, hostname string, ip4 net.IP, ip6 net.IP) erro log.Printf("Sending DNS Update: %v", msg) - reply, _, err := dnsClient.Exchange(msg, C.DNSServer) + reply, _, err := dnsClient.Exchange(msg, C.DNS.Server) if err != nil { return fmt.Errorf("failed to execute DNS Udpate: %v", err) } diff --git a/service/userconfig.go b/service/userconfig.go index 55f06cf..dfb9968 100644 --- a/service/userconfig.go +++ b/service/userconfig.go @@ -45,7 +45,7 @@ type UserConfigNFT struct { } func LoadConfigForUser(username string, password string) (*UserConfig, error) { - configFile := fmt.Sprintf("%s/%s.yml", C.UsersConfigDir, username) + configFile := fmt.Sprintf("%s/%s.yml", C.Users.ConfigDir, username) configFile = path.Clean(configFile) log.Printf("Trying to load config file %s for user %s", configFile, username) diff --git a/webapi/config.go b/webapi/config.go index 816384f..f5d5871 100644 --- a/webapi/config.go +++ b/webapi/config.go @@ -1,6 +1,9 @@ package webapi import ( + "encoding/json" + "log" + "github.com/spf13/viper" ) @@ -19,5 +22,13 @@ func SetConfigDefaults() { } func LoadConfig() { - C.ListenAddress = viper.GetString("WebAPI.ListenAddress") + viper.UnmarshalKey("WebAPI", &C) +} + +func (obj *config) PrettyPrint() string { + s, err := json.MarshalIndent(obj, "", " ") + if err != nil { + log.Fatalf("Failed to pretty print WebAPI Config via JSON: %v", err) + } + return string(s) } diff --git a/webapi/server.go b/webapi/server.go index a5b7499..e1b3e34 100644 --- a/webapi/server.go +++ b/webapi/server.go @@ -63,4 +63,8 @@ func handleUpdate(w http.ResponseWriter, r *http.Request) { log.Printf("Request PW: %s, Config PW: %s", ur.Password, v.GetString("password")) fmt.Fprintln(w, "Unmarshalled userconfig:") fmt.Fprintln(w, uc.PrettyPrint()) + fmt.Fprintln(w, "WebAPI Config:") + fmt.Fprintln(w, C.PrettyPrint()) + fmt.Fprintln(w, "Service Config:") + fmt.Fprintln(w, service.C.PrettyPrint()) }