- Added sample default config (needs manual updating)

- Reworked service and webapi config to Viper unmarshalling
- Added Pretty Printers to config
This commit is contained in:
Torben Nehmer 2021-08-22 14:46:48 +02:00
parent 9a31bbc912
commit 7b93ff425d
6 changed files with 54 additions and 12 deletions

8
default-config.yml Normal file
View File

@ -0,0 +1,8 @@
service:
dns:
defaultttl: 60
server: 10.10.11.254:53
users:
configdir: users/
webapi:
listenaddress: :8080

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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())
}