diff options
| author | Shulhan <ms@kilabit.info> | 2022-04-19 23:57:51 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-04-19 23:57:51 +0700 |
| commit | 7d7b0b0fc9eb078001a010dcdab3aa97938806c3 (patch) | |
| tree | 8f2b97a227abae8a503fb887598b666ab53e90b3 | |
| parent | b5a14cefdf1a16d0ec23e7231f776f3f07352fd6 (diff) | |
| download | rescached-7d7b0b0fc9eb078001a010dcdab3aa97938806c3.tar.xz | |
all: always call Environment init
This is to make sure that all required fields that are empty in the
configuration initialized to its default values.
| -rw-r--r-- | environment.go | 28 | ||||
| -rw-r--r-- | environment_test.go | 2 |
2 files changed, 15 insertions, 15 deletions
diff --git a/environment.go b/environment.go index 99dc773..fad9c4e 100644 --- a/environment.go +++ b/environment.go @@ -20,7 +20,8 @@ import ( ) const ( - defWuiAddress = "127.0.0.1:5380" + defListenAddress = "127.0.0.1:53" + defWuiAddress = "127.0.0.1:5380" ) const ( @@ -95,20 +96,19 @@ func LoadEnvironment(dirBase, fileConfig string) (env *Environment, err error) { env = newEnvironment(dirBase, fileConfig) - if len(fileConfig) == 0 { - _ = env.init() - return env, nil - } + if len(fileConfig) > 0 { + cfg, err = ini.Open(env.fileConfig) + if err != nil { + return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) + } - cfg, err = ini.Open(env.fileConfig) - if err != nil { - return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) + err = cfg.Unmarshal(env) + if err != nil { + return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) + } } - err = cfg.Unmarshal(env) - if err != nil { - return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err) - } + _ = env.init() return env, nil } @@ -130,7 +130,7 @@ func newEnvironment(dirBase, fileConfig string) *Environment { Address: defWuiAddress, }, ServerOptions: dns.ServerOptions{ - ListenAddress: "127.0.0.1:53", + ListenAddress: defListenAddress, }, } } @@ -141,7 +141,7 @@ func (env *Environment) init() (err error) { env.WUIListen = defWuiAddress } if len(env.ListenAddress) == 0 { - env.ListenAddress = "127.0.0.1:53" + env.ListenAddress = defListenAddress } if len(env.FileResolvConf) > 0 { _, _ = env.loadResolvConf() diff --git a/environment_test.go b/environment_test.go index e7dddb0..43f932e 100644 --- a/environment_test.go +++ b/environment_test.go @@ -88,7 +88,7 @@ func TestLoadEnvironment(t *testing.T) { }, }, HttpdOptions: &libhttp.ServerOptions{ - Address: defWuiAddress, + Address: "127.0.0.1:5381", }, ServerOptions: dns.ServerOptions{ ListenAddress: "127.0.0.1:5350", |
