diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-05-18 23:57:56 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-07-26 03:48:51 +0700 |
| commit | 03ab6a462ddbd7589ac385a219f035ca7975f856 (patch) | |
| tree | ac26781fb0246021ce5ba4bbf69aaf1055e5c6bb | |
| parent | 7574bb6ebff609b9a2889fc573ce3edce256e030 (diff) | |
| download | rescached-03ab6a462ddbd7589ac385a219f035ca7975f856.tar.xz | |
all: rename Options to environment
While at it, unexport it.
| -rw-r--r-- | environment.go (renamed from options.go) | 88 | ||||
| -rw-r--r-- | environment_test.go (renamed from options_test.go) | 12 | ||||
| -rw-r--r-- | rescached.go | 24 | ||||
| -rw-r--r-- | rescached_httpd.go | 14 |
4 files changed, 69 insertions, 69 deletions
diff --git a/options.go b/environment.go index 93fb499..2cbe0e6 100644 --- a/options.go +++ b/environment.go @@ -43,9 +43,9 @@ const ( ) // -// Options for running rescached. +// environment for running rescached. // -type Options struct { +type environment struct { dns.ServerOptions WuiListen string `ini:"rescached::wui.listen"` DirHosts string `ini:"rescached::dir.hosts"` @@ -54,37 +54,37 @@ type Options struct { Debug int `ini:"rescached::debug"` } -func loadOptions(file string) (opts *Options) { - opts = NewOptions() +func loadEnvironment(file string) (env *environment) { + env = newEnvironment() if len(file) == 0 { - opts.init() - return opts + env.init() + return env } cfg, err := ioutil.ReadFile(file) if err != nil { - log.Printf("rescached: loadOptions %q: %s", file, err) - return opts + log.Printf("rescached: loadEnvironment %q: %s", file, err) + return env } - err = ini.Unmarshal(cfg, opts) + err = ini.Unmarshal(cfg, env) if err != nil { - log.Printf("rescached: loadOptions %q: %s", file, err) - return opts + log.Printf("rescached: loadEnvironment %q: %s", file, err) + return env } - opts.init() - debug.Value = opts.Debug + env.init() + debug.Value = env.Debug - return opts + return env } // -// NewOptions create and initialize options with default values. +// newEnvironment create and initialize options with default values. // -func NewOptions() *Options { - return &Options{ +func newEnvironment() *environment { + return &environment{ ServerOptions: dns.ServerOptions{ ListenAddress: "127.0.0.1:53", }, @@ -92,22 +92,22 @@ func NewOptions() *Options { } // -// init check and initialize the Options instance with default values. +// init check and initialize the environment instance with default values. // -func (opts *Options) init() { - if len(opts.WuiListen) == 0 { - opts.WuiListen = defWuiAddress +func (env *environment) init() { + if len(env.WuiListen) == 0 { + env.WuiListen = defWuiAddress } - if len(opts.ListenAddress) == 0 { - opts.ListenAddress = "127.0.0.1:53" + if len(env.ListenAddress) == 0 { + env.ListenAddress = "127.0.0.1:53" } - if len(opts.FileResolvConf) > 0 { - _, _ = opts.loadResolvConf() + if len(env.FileResolvConf) > 0 { + _, _ = env.loadResolvConf() } } -func (opts *Options) loadResolvConf() (ok bool, err error) { - rc, err := libnet.NewResolvConf(opts.FileResolvConf) +func (env *environment) loadResolvConf() (ok bool, err error) { + rc, err := libnet.NewResolvConf(env.FileResolvConf) if err != nil { return false, err } @@ -124,14 +124,14 @@ func (opts *Options) loadResolvConf() (ok bool, err error) { rc.NameServers[x] = "udp://" + rc.NameServers[x] } - if libstrings.IsEqual(opts.NameServers, rc.NameServers) { + if libstrings.IsEqual(env.NameServers, rc.NameServers) { return false, nil } - if len(opts.NameServers) == 0 { - opts.NameServers = rc.NameServers + if len(env.NameServers) == 0 { + env.NameServers = rc.NameServers } else { - opts.FallbackNS = rc.NameServers + env.FallbackNS = rc.NameServers } return true, nil @@ -140,41 +140,41 @@ func (opts *Options) loadResolvConf() (ok bool, err error) { // // write the options values back to file. // -func (opts *Options) 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) } - in.Set(sectionNameRescached, "", keyFileResolvConf, opts.FileResolvConf) - in.Set(sectionNameRescached, "", keyDebug, strconv.Itoa(opts.Debug)) + in.Set(sectionNameRescached, "", keyFileResolvConf, env.FileResolvConf) + in.Set(sectionNameRescached, "", keyDebug, strconv.Itoa(env.Debug)) in.UnsetAll(sectionNameDNS, subNameServer, keyParent) - for _, ns := range opts.NameServers { + for _, ns := range env.NameServers { in.Add(sectionNameDNS, subNameServer, keyParent, ns) } in.Set(sectionNameDNS, subNameServer, keyListen, - opts.ServerOptions.ListenAddress) + env.ServerOptions.ListenAddress) in.Set(sectionNameDNS, subNameServer, keyHTTPPort, - strconv.Itoa(int(opts.ServerOptions.HTTPPort))) + strconv.Itoa(int(env.ServerOptions.HTTPPort))) in.Set(sectionNameDNS, subNameServer, keyTLSPort, - strconv.Itoa(int(opts.ServerOptions.TLSPort))) + strconv.Itoa(int(env.ServerOptions.TLSPort))) in.Set(sectionNameDNS, subNameServer, keyTLSCertificate, - opts.ServerOptions.TLSCertFile) + env.ServerOptions.TLSCertFile) in.Set(sectionNameDNS, subNameServer, keyTLSPrivateKey, - opts.ServerOptions.TLSPrivateKey) + env.ServerOptions.TLSPrivateKey) in.Set(sectionNameDNS, subNameServer, keyTLSAllowInsecure, - fmt.Sprintf("%t", opts.ServerOptions.TLSAllowInsecure)) + fmt.Sprintf("%t", env.ServerOptions.TLSAllowInsecure)) in.Set(sectionNameDNS, subNameServer, keyDohBehindProxy, - fmt.Sprintf("%t", opts.ServerOptions.DoHBehindProxy)) + fmt.Sprintf("%t", env.ServerOptions.DoHBehindProxy)) in.Set(sectionNameDNS, subNameServer, keyCachePruneDelay, - fmt.Sprintf("%s", opts.ServerOptions.PruneDelay)) + fmt.Sprintf("%s", env.ServerOptions.PruneDelay)) in.Set(sectionNameDNS, subNameServer, keyCachePruneThreshold, - fmt.Sprintf("%s", opts.ServerOptions.PruneThreshold)) + fmt.Sprintf("%s", env.ServerOptions.PruneThreshold)) return in.Save(file) } diff --git a/options_test.go b/environment_test.go index 0cd78d5..19fd80c 100644 --- a/options_test.go +++ b/environment_test.go @@ -12,15 +12,15 @@ import ( "github.com/shuLhan/share/lib/test" ) -func TestOptions(t *testing.T) { +func TestEnvironment(t *testing.T) { cases := []struct { desc string content string - exp *Options + exp *environment expError string }{{ desc: "With empty content", - exp: &Options{}, + 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: &Options{ + 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 := &Options{ + got := &environment{ ServerOptions: dns.ServerOptions{}, } @@ -52,6 +52,6 @@ parent = https://kilabit.info/dns-query continue } - test.Assert(t, "Options", c.exp, got, true) + test.Assert(t, "environment", c.exp, got, true) } } diff --git a/rescached.go b/rescached.go index 74fc949..bf76b49 100644 --- a/rescached.go +++ b/rescached.go @@ -20,7 +20,7 @@ import ( type Server struct { fileConfig string dns *dns.Server - opts *Options + env *environment rcWatcher *libio.Watcher httpd *http.Server @@ -31,15 +31,15 @@ type Server struct { // New create and initialize new rescached server. // func New(fileConfig string) (srv *Server, err error) { - opts := loadOptions(fileConfig) + env := loadEnvironment(fileConfig) if debug.Value >= 1 { - fmt.Printf("rescached: config: %+v\n", opts) + fmt.Printf("rescached: config: %+v\n", env) } srv = &Server{ fileConfig: fileConfig, - opts: opts, + env: env, } err = srv.httpdInit() @@ -55,18 +55,18 @@ func New(fileConfig string) (srv *Server, err error) { // it. // func (srv *Server) Start() (err error) { - srv.dns, err = dns.NewServer(&srv.opts.ServerOptions) + srv.dns, err = dns.NewServer(&srv.env.ServerOptions) if err != nil { return err } - srv.dns.LoadHostsDir(srv.opts.DirHosts) - srv.dns.LoadMasterDir(srv.opts.DirMaster) + srv.dns.LoadHostsDir(srv.env.DirHosts) + srv.dns.LoadMasterDir(srv.env.DirMaster) srv.dns.LoadHostsFile("") - if len(srv.opts.FileResolvConf) > 0 { + if len(srv.env.FileResolvConf) > 0 { srv.rcWatcher, err = libio.NewWatcher( - srv.opts.FileResolvConf, 0, srv.watchResolvConf) + srv.env.FileResolvConf, 0, srv.watchResolvConf) if err != nil { log.Fatal("rescached: Start:", err) } @@ -108,10 +108,10 @@ func (srv *Server) Stop() { func (srv *Server) watchResolvConf(ns *libio.NodeState) { switch ns.State { case libio.FileStateDeleted: - log.Printf("= ResolvConf: file %q deleted\n", srv.opts.FileResolvConf) + log.Printf("= ResolvConf: file %q deleted\n", srv.env.FileResolvConf) return default: - ok, err := srv.opts.loadResolvConf() + ok, err := srv.env.loadResolvConf() if err != nil { log.Println("rescached: loadResolvConf: " + err.Error()) break @@ -120,6 +120,6 @@ func (srv *Server) watchResolvConf(ns *libio.NodeState) { break } - srv.dns.RestartForwarders(srv.opts.NameServers, srv.opts.FallbackNS) + srv.dns.RestartForwarders(srv.env.NameServers, srv.env.FallbackNS) } } diff --git a/rescached_httpd.go b/rescached_httpd.go index 4ded760..c875ff1 100644 --- a/rescached_httpd.go +++ b/rescached_httpd.go @@ -19,9 +19,9 @@ const ( ) func (srv *Server) httpdInit() (err error) { - opts := &http.ServerOptions{ + env := &http.ServerOptions{ Root: defHTTPDRootDir, - Address: srv.opts.WuiListen, + Address: srv.env.WuiListen, Includes: []string{ `.*\.css`, `.*\.html`, @@ -35,7 +35,7 @@ func (srv *Server) httpdInit() (err error) { }, } - srv.httpd, err = http.NewServer(opts) + srv.httpd, err = http.NewServer(env) if err != nil { return fmt.Errorf("newHTTPServer: %w", err) } @@ -86,7 +86,7 @@ func (srv *Server) httpdRun() { } }() - log.Printf("=== rescached: httpd listening at %s", srv.opts.WuiListen) + log.Printf("=== rescached: httpd listening at %s", srv.env.WuiListen) err := srv.httpd.Start() if err != nil { @@ -99,7 +99,7 @@ func (srv *Server) httpdAPIGetEnvironment( ) ( resBody []byte, err error, ) { - return json.Marshal(srv.opts) + return json.Marshal(srv.env) } func (srv *Server) httpdAPIPostEnvironment( @@ -107,7 +107,7 @@ func (srv *Server) httpdAPIPostEnvironment( ) ( resBody []byte, err error, ) { - newOpts := new(Options) + newOpts := new(environment) err = json.Unmarshal(reqBody, newOpts) if err != nil { return nil, err @@ -130,7 +130,7 @@ func (srv *Server) httpdAPIPostEnvironment( return json.Marshal(res) } - srv.opts = newOpts + srv.env = newOpts srv.Stop() err = srv.Start() |
