dyndns/service/config.go
Torben Nehmer a23043ba5f Added state perstistance
- Define new state directory to hold the last dns update config and push it into the config, default to state/
- Refactoring, move updaterequest into service and out of webapp. Keep only the form parsing there. Specifically, move the processing code into the service package, it shouldn't be in the webapp.
- Add code to load and save the last updaterequest into a state file (watch out to keep the possibly unhashed password out of it).
- Add a command line option to load all available state files given the current configured user list from the command line.
- Refactor user configuartion loading to separate it from user authentication, claen up the code.
- We now require the username in the config, to make handling it in state updates easier. This will allow easier transition to DB style storage anyway.
- Update .gitignore
2021-09-18 15:38:49 +02:00

67 lines
1.5 KiB
Go

package service
import (
"encoding/json"
"log"
"path/filepath"
"github.com/spf13/viper"
)
type config struct {
DNS configDNS
Users configUsers
}
type configDNS struct {
Server string
DefaultTTL uint32
}
type configUsers struct {
ConfigDir string
StateDir string
}
var C config
func init() {
SetConfigDefaults()
}
func SetConfigDefaults() {
viper.SetDefault("Service.DNS.Server", "127.0.0.1:53")
viper.SetDefault("Service.DNS.DefaultTTL", 60)
viper.SetDefault("Service.Users.ConfigDir", "users/")
viper.SetDefault("Service.Users.StateDir", "state/")
}
func LoadConfig() {
// Does not work with partially overrides, only GetXXX does resolve the subkeys correctly
// https://github.com/spf13/viper/issues/798
// https://github.com/spf13/viper/issues/309
// viper.UnmarshalKey("Service", &C)
C.DNS.Server = viper.GetString("Service.DNS.Server")
C.DNS.DefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL")
path := viper.GetString("Service.Users.ConfigDir")
if filepath.IsAbs(path) {
C.Users.ConfigDir = filepath.Clean(path)
} else {
C.Users.ConfigDir = filepath.Join(viper.GetString("ConfigDir"), path)
}
path = viper.GetString("Service.Users.StateDir")
if filepath.IsAbs(path) {
C.Users.StateDir = filepath.Clean(path)
} else {
C.Users.StateDir = filepath.Join(viper.GetString("StateDir"), path)
}
}
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)
}