diff options
Diffstat (limited to 'server_config.go')
| -rw-r--r-- | server_config.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/server_config.go b/server_config.go new file mode 100644 index 0000000..50d93ff --- /dev/null +++ b/server_config.go @@ -0,0 +1,80 @@ +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-only + +package lilin + +import ( + "fmt" + "os" + "path/filepath" + + "git.sr.ht/~shulhan/pakakeh.go/lib/ini" +) + +// ServerConfig define the options for Lilin Server. +type ServerConfig struct { + // BaseDir define the base directory for running the server, default + // to "/". + // Its affect where to read the configuration, storing states, and + // logs. + BaseDir string + + // The BaseDir + "/etc/lilin/". + configDir string + + // The BaseDir + "/etc/lilin/service.d/". + configServiceDir string + + // The BaseDir + "/var/log/lilin/service.d/". + logServiceDir string + + // The address to listen for HTTP server and APIs. + Address string `ini:"server::address"` + + // IsDevelopment run the server in development mode with direct access + // to file system in _www instead of using [embed.FS]. + IsDevelopment bool +} + +// init initialize the server by reading the configuration from +// [ServerConfig.BaseDir]/etc/lilin/lilin.cfg. +func (cfg *ServerConfig) init() (err error) { + var logp = `init` + + if cfg.BaseDir == `` { + cfg.BaseDir = `/` + } + + cfg.configDir = filepath.Join(cfg.BaseDir, `etc`, `lilin`) + cfg.configServiceDir = filepath.Join(cfg.configDir, `service.d`) + cfg.logServiceDir = filepath.Join(cfg.BaseDir, `var`, `log`, `lilin`, `service.d`) + + err = os.MkdirAll(cfg.logServiceDir, 0700) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + // Load main configuration. + + var fileCfg = filepath.Join(cfg.configDir, `lilin.cfg`) + _, err = os.Stat(fileCfg) + if err != nil { + return nil + } + + var rawconf []byte + rawconf, err = os.ReadFile(fileCfg) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + err = ini.Unmarshal(rawconf, cfg) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + if cfg.Address == `` { + cfg.Address = defAddress + } + + return nil +} |
