aboutsummaryrefslogtreecommitdiff
path: root/environment.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-19 23:18:19 +0700
committerShulhan <ms@kilabit.info>2022-04-19 23:30:09 +0700
commited83581fc716d5d41d769662fae0bc5e78309982 (patch)
tree8f11ad99ee9ef2dfd595c485d01425fd1affb574 /environment.go
parent2ada0a2da129a33229d63462b2c7fa7ccd5d0460 (diff)
downloadrescached-ed83581fc716d5d41d769662fae0bc5e78309982.tar.xz
all: add option to set the base directory
Previously, running an instance of rescached assume that all configurations and cache are located in /etc/rescached and /var/cache/rescached. It possible that we may want to use different base directory (or $PREFIX, in the auto tools terms), for example "/opt/rescached" or as showed in the "dev" task in the Makefile, we use "_test" as base directory for running test instance. This changes also fix an error when loading hosts file from non-existant hosts.d directory.
Diffstat (limited to 'environment.go')
-rw-r--r--environment.go42
1 files changed, 29 insertions, 13 deletions
diff --git a/environment.go b/environment.go
index aac538e..99dc773 100644
--- a/environment.go
+++ b/environment.go
@@ -6,6 +6,7 @@ package rescached
import (
"fmt"
"io"
+ "path/filepath"
"strconv"
"strings"
@@ -46,9 +47,12 @@ const (
keyTLSPort = "tls.port"
keyTLSPrivateKey = "tls.private_key"
- dirBlock = "/etc/rescached/block.d"
- dirHosts = "/etc/rescached/hosts.d"
- dirZone = "/etc/rescached/zone.d"
+ dirBlock = "/etc/rescached/block.d"
+ dirCaches = "/var/cache/rescached/"
+ dirHosts = "/etc/rescached/hosts.d"
+ dirZone = "/etc/rescached/zone.d"
+
+ fileCaches = "rescached.gob"
)
var (
@@ -60,6 +64,13 @@ type Environment struct {
HostsFiles map[string]*dns.HostsFile
Zones map[string]*dns.Zone
+ dirBase string
+ pathDirBlock string
+ pathDirCaches string
+ pathDirHosts string
+ pathDirZone string
+ pathFileCaches string
+
fileConfig string
FileResolvConf string `ini:"rescached::file.resolvconf"`
WUIListen string `ini:"rescached::wui.listen"`
@@ -76,36 +87,43 @@ type Environment struct {
}
// LoadEnvironment initialize environment from configuration file.
-func LoadEnvironment(fileConfig string) (env *Environment, err error) {
+func LoadEnvironment(dirBase, fileConfig string) (env *Environment, err error) {
var (
logp = "LoadEnvironment"
cfg *ini.Ini
)
- env = newEnvironment()
- env.fileConfig = fileConfig
+ env = newEnvironment(dirBase, fileConfig)
if len(fileConfig) == 0 {
_ = env.init()
return env, nil
}
- cfg, err = ini.Open(fileConfig)
+ cfg, err = ini.Open(env.fileConfig)
if err != nil {
- return nil, fmt.Errorf("%s: %q: %s", logp, fileConfig, err)
+ 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, fileConfig, err)
+ return nil, fmt.Errorf("%s: %q: %s", logp, env.fileConfig, err)
}
return env, nil
}
// newEnvironment create and initialize options with default values.
-func newEnvironment() *Environment {
+func newEnvironment(dirBase, fileConfig string) *Environment {
return &Environment{
+ dirBase: dirBase,
+ pathDirBlock: filepath.Join(dirBase, dirBlock),
+ pathDirCaches: filepath.Join(dirBase, dirCaches),
+ pathDirHosts: filepath.Join(dirBase, dirHosts),
+ pathDirZone: filepath.Join(dirBase, dirZone),
+ pathFileCaches: filepath.Join(dirBase, dirCaches, fileCaches),
+
+ fileConfig: filepath.Join(dirBase, fileConfig),
hostsBlocksFile: make(map[string]*dns.HostsFile),
HttpdOptions: &libhttp.ServerOptions{
Memfs: mfsWww,
@@ -169,12 +187,10 @@ func (env *Environment) init() (err error) {
func (env *Environment) initHostsBlock() {
var (
- dirBase = ""
-
hb *hostsBlock
)
for _, hb = range env.HostsBlocks {
- hb.init(dirBase)
+ hb.init(env.pathDirBlock)
}
}