diff options
| author | Shulhan <ms@kilabit.info> | 2021-12-21 08:56:21 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-12-21 08:56:21 +0700 |
| commit | 51af8aca85c8b2a264d99d3929b4259aa4717e7a (patch) | |
| tree | ab7bf63472b8f92d8709190600a71f7882245a44 /environment.go | |
| parent | a29a8c8396fc16af744a0abc86b5aca343aa620a (diff) | |
| download | rescached-51af8aca85c8b2a264d99d3929b4259aa4717e7a.tar.xz | |
all: move HTTP server option to Environment
Previously, the options for HTTP server initialized internally, which
make it uneasy to changes the default rescached instance when running
in development mode.
This commit move the HTTP server initialization inside the Environment
init method. If its nil, the init will set the default HTTP server
options along with the Memfs.
Diffstat (limited to 'environment.go')
| -rw-r--r-- | environment.go | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/environment.go b/environment.go index 3d1bede..71c7144 100644 --- a/environment.go +++ b/environment.go @@ -11,7 +11,9 @@ import ( "github.com/shuLhan/share/lib/debug" "github.com/shuLhan/share/lib/dns" + libhttp "github.com/shuLhan/share/lib/http" "github.com/shuLhan/share/lib/ini" + "github.com/shuLhan/share/lib/memfs" libnet "github.com/shuLhan/share/lib/net" libstrings "github.com/shuLhan/share/lib/strings" ) @@ -49,6 +51,10 @@ const ( dirZone = "/etc/rescached/zone.d" ) +var ( + mfsWww *memfs.MemFS +) + // // Environment for running rescached. // @@ -63,6 +69,9 @@ type Environment struct { HostsBlocksRaw []string `ini:"rescached::hosts_block" json:"-"` HostsBlocks []*hostsBlock + // The options for WUI HTTP server. + HttpdOptions *libhttp.ServerOptions `json:"-"` + dns.ServerOptions Debug int `ini:"rescached::debug"` @@ -77,7 +86,8 @@ func LoadEnvironment(fileConfig string) (env *Environment, err error) { cfg *ini.Ini ) - env = newEnvironment(fileConfig) + env = newEnvironment() + env.fileConfig = fileConfig if len(fileConfig) == 0 { env.init() @@ -94,29 +104,28 @@ func LoadEnvironment(fileConfig string) (env *Environment, err error) { return nil, fmt.Errorf("%s: %q: %s", logp, fileConfig, err) } - env.init() - env.initHostsBlock(cfg) - debug.Value = env.Debug - return env, nil } // // newEnvironment create and initialize options with default values. // -func newEnvironment(fileConfig string) *Environment { +func newEnvironment() *Environment { return &Environment{ + HttpdOptions: &libhttp.ServerOptions{ + Memfs: mfsWww, + Address: defWuiAddress, + }, 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() (err error) { if len(env.WUIListen) == 0 { env.WUIListen = defWuiAddress } @@ -126,9 +135,43 @@ func (env *Environment) init() { if len(env.FileResolvConf) > 0 { _, _ = env.loadResolvConf() } + + debug.Value = env.Debug + + if env.HttpdOptions == nil { + env.HttpdOptions = &libhttp.ServerOptions{ + Memfs: mfsWww, + Address: env.WUIListen, + } + } else { + env.HttpdOptions.Address = env.WUIListen + } + + if env.HttpdOptions.Memfs == nil { + memfsWwwOpts := &memfs.Options{ + Root: defHTTPDRootDir, + Includes: []string{ + `.*\.css$`, + `.*\.html$`, + `.*\.js$`, + `.*\.png$`, + }, + Embed: memfs.EmbedOptions{ + PackageName: "rescached", + VarName: "mfsWww", + GoFileName: "memfs_generate.go", + }, + } + env.HttpdOptions.Memfs, err = memfs.New(memfsWwwOpts) + if err != nil { + return fmt.Errorf("Environment.init: %w", err) + } + } + + return nil } -func (env *Environment) initHostsBlock(cfg *ini.Ini) { +func (env *Environment) initHostsBlock() { env.HostsBlocks = hostsBlockSources for x, v := range env.HostsBlocksRaw { |
