rewrite most code; removes database to solely rely on config file

This commit is contained in:
Thomas Preisner 2021-07-13 13:36:37 +02:00
parent 32bf03dc07
commit ef22e29cef
10 changed files with 246 additions and 249 deletions

48
config.go Normal file
View file

@ -0,0 +1,48 @@
package main
import (
"fmt"
"github.com/pelletier/go-toml"
"os"
)
type Config struct {
ServerPort uint16 `toml:"port"`
RRConfigs []RRConfig `toml:"rrconfig"`
rrconfigs map[string]*RRConfig
}
type RRConfig struct {
Username string `toml:"username"`
Password string `toml:"password"`
Nameserver string `toml:"nameserver"`
Zonename string `toml:"zonename"`
Hostname string `toml:"hostname"`
Tsigkey string `toml:"tsigkey"`
Ttl int `toml:"ttl" default:"60"`
}
func LoadConfig(path string) (*Config, error) {
var cfg Config
f, err := os.Open(path)
if err != nil {
return nil, err
}
decoder := toml.NewDecoder(f).Strict(true)
err = decoder.Decode(&cfg)
if err != nil {
return nil, err
}
cfg.rrconfigs = map[string]*RRConfig{}
for _, entry := range cfg.RRConfigs {
if _, ok := cfg.rrconfigs[entry.Username]; ok {
return nil, fmt.Errorf("Duplicate username detected")
}
cfg.rrconfigs[entry.Username] = &entry
}
return &cfg, nil
}