diff options
| -rw-r--r-- | cmd/rescached/main.go | 7 | ||||
| -rw-r--r-- | environment.go | 50 | ||||
| -rw-r--r-- | environment_test.go | 8 | ||||
| -rw-r--r-- | httpd.go | 6 | ||||
| -rw-r--r-- | rescached.go | 14 |
5 files changed, 47 insertions, 38 deletions
diff --git a/cmd/rescached/main.go b/cmd/rescached/main.go index a43eefd..ebf0a62 100644 --- a/cmd/rescached/main.go +++ b/cmd/rescached/main.go @@ -29,7 +29,12 @@ func main() { flag.StringVar(&fileConfig, "config", "", "path to configuration") flag.Parse() - rcd, err := rescached.New(fileConfig) + env, err := rescached.LoadEnvironment(fileConfig) + if err != nil { + log.Fatal(err) + } + + rcd, err := rescached.New(env) if err != nil { log.Fatal(err) } diff --git a/environment.go b/environment.go index fdf4bd4..21cefd9 100644 --- a/environment.go +++ b/environment.go @@ -6,7 +6,6 @@ package rescached import ( "fmt" - "log" "strconv" "strings" @@ -51,12 +50,14 @@ const ( ) // -// environment for running rescached. +// Environment for running rescached. // -type environment struct { +type Environment struct { dns.ServerOptions - WUIListen string `ini:"rescached::wui.listen"` - FileResolvConf string `ini:"rescached::file.resolvconf"` + WUIListen string `ini:"rescached::wui.listen"` + FileResolvConf string `ini:"rescached::file.resolvconf"` + fileConfig string + Debug int `ini:"rescached::debug"` HostsBlocksRaw []string `ini:"rescached::hosts_block" json:"-"` HostsBlocks []*hostsBlock @@ -64,48 +65,55 @@ type environment struct { Zones map[string]*dns.Zone } -func loadEnvironment(file string) (env *environment) { - env = newEnvironment() +// +// LoadEnvironment initialize environment from configuration file. +// +func LoadEnvironment(fileConfig string) (env *Environment, err error) { + var ( + logp = "LoadEnvironment" + cfg *ini.Ini + ) + + env = newEnvironment(fileConfig) - if len(file) == 0 { + if len(fileConfig) == 0 { env.init() - return env + return env, nil } - cfg, err := ini.Open(file) + cfg, err = ini.Open(fileConfig) if err != nil { - log.Printf("loadEnvironment %q: %s", file, err) - return env + return nil, fmt.Errorf("%s: %q: %s", logp, fileConfig, err) } err = cfg.Unmarshal(env) if err != nil { - log.Printf("loadEnvironment %q: %s", file, err) - return env + return nil, fmt.Errorf("%s: %q: %s", logp, fileConfig, err) } env.init() env.initHostsBlock(cfg) debug.Value = env.Debug - return env + return env, nil } // // newEnvironment create and initialize options with default values. // -func newEnvironment() *environment { - return &environment{ +func newEnvironment(fileConfig string) *Environment { + return &Environment{ ServerOptions: dns.ServerOptions{ ListenAddress: "127.0.0.1:53", }, + fileConfig: fileConfig, } } // // init check and initialize the environment instance with default values. // -func (env *environment) init() { +func (env *Environment) init() { if len(env.WUIListen) == 0 { env.WUIListen = defWuiAddress } @@ -117,7 +125,7 @@ func (env *environment) init() { } } -func (env *environment) initHostsBlock(cfg *ini.Ini) { +func (env *Environment) initHostsBlock(cfg *ini.Ini) { env.HostsBlocks = hostsBlockSources for x, v := range env.HostsBlocksRaw { @@ -129,7 +137,7 @@ func (env *environment) initHostsBlock(cfg *ini.Ini) { } } -func (env *environment) loadResolvConf() (ok bool, err error) { +func (env *Environment) loadResolvConf() (ok bool, err error) { rc, err := libnet.NewResolvConf(env.FileResolvConf) if err != nil { return false, err @@ -161,7 +169,7 @@ func (env *environment) loadResolvConf() (ok bool, err error) { // // write the options values back to file. // -func (env *environment) write(file string) (err error) { +func (env *Environment) write(file string) (err error) { in, err := ini.Open(file) if err != nil { return fmt.Errorf("write: %w", err) diff --git a/environment_test.go b/environment_test.go index 985df1d..c9278a7 100644 --- a/environment_test.go +++ b/environment_test.go @@ -16,11 +16,11 @@ func TestEnvironment(t *testing.T) { cases := []struct { desc string content string - exp *environment + exp *Environment expError string }{{ desc: "With empty content", - exp: &environment{}, + exp: &Environment{}, }, { desc: "With multiple parents", content: `[dns "server"] @@ -28,7 +28,7 @@ listen = 127.0.0.1:53 parent = udp://35.240.172.103 parent = https://kilabit.info/dns-query `, - exp: &environment{ + exp: &Environment{ ServerOptions: dns.ServerOptions{ ListenAddress: "127.0.0.1:53", NameServers: []string{ @@ -42,7 +42,7 @@ parent = https://kilabit.info/dns-query for _, c := range cases { t.Log(c.desc) - got := &environment{ + got := &Environment{ ServerOptions: dns.ServerOptions{}, } @@ -356,7 +356,7 @@ func (srv *Server) httpdAPIPostEnvironment(epr *libhttp.EndpointRequest) (resBod }, } - newOpts := new(environment) + newOpts := new(Environment) err = json.Unmarshal(epr.RequestBody, newOpts) if err != nil { return nil, err @@ -372,7 +372,7 @@ func (srv *Server) httpdAPIPostEnvironment(epr *libhttp.EndpointRequest) (resBod fmt.Printf("new options: %+v\n", newOpts) - err = newOpts.write(srv.fileConfig) + err = newOpts.write(srv.env.fileConfig) if err != nil { res.Code = http.StatusInternalServerError res.Message = err.Error() @@ -442,7 +442,7 @@ func (srv *Server) apiHostsBlockUpdate(epr *libhttp.EndpointRequest) (resBody [] } } - err = srv.env.write(srv.fileConfig) + err = srv.env.write(srv.env.fileConfig) if err != nil { log.Println("apiHostsBlockUpdate:", err.Error()) res.Message = err.Error() diff --git a/rescached.go b/rescached.go index af00a1b..a283826 100644 --- a/rescached.go +++ b/rescached.go @@ -26,10 +26,9 @@ const ( // Server implement caching DNS server. type Server struct { - fileConfig string - dns *dns.Server - env *environment - rcWatcher *libio.Watcher + dns *dns.Server + env *Environment + rcWatcher *libio.Watcher httpd *http.Server httpdRunner sync.Once @@ -38,16 +37,13 @@ type Server struct { // // New create and initialize new rescached server. // -func New(fileConfig string) (srv *Server, err error) { - env := loadEnvironment(fileConfig) - +func New(env *Environment) (srv *Server, err error) { if debug.Value >= 1 { fmt.Printf("--- rescached: config: %+v\n", env) } srv = &Server{ - fileConfig: fileConfig, - env: env, + env: env, } err = srv.httpdInit() |
