aboutsummaryrefslogtreecommitdiff
path: root/environment.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-12-19 14:40:18 +0700
committerShulhan <ms@kilabit.info>2021-12-19 14:40:18 +0700
commit7ee3e4ea4658e85a40540212b95206b09686feb7 (patch)
treebabe6a3568f6ead05799e0af31c6d5f6851cfa0b /environment.go
parent7713203c6bd41053599732869a70467a168e4e55 (diff)
downloadrescached-7ee3e4ea4658e85a40540212b95206b09686feb7.tar.xz
all: refactoring Server initialization
Previously, we initialize the rescached Server by passing the path to configuration file and initialize the environment inside the server. This commit changes it to pass the instance of Environment. The idea is to allow creating and running rescached Server without creating configuration file first.
Diffstat (limited to 'environment.go')
-rw-r--r--environment.go50
1 files changed, 29 insertions, 21 deletions
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)