aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/service/config.go
diff options
context:
space:
mode:
authorSlack Coder <slackcoder@server.ky>2024-10-23 06:07:32 -0500
committerSlack Coder <slackcoder@server.ky>2024-10-23 06:14:49 -0500
commitca3a097505c42c2aec16e208c571c1b496c4b14b (patch)
treeaa1020524d6166ad2e527c15864f42e0ce15301b /internal/service/config.go
parentc8ba7263e7923fc76eee7dbe129a8bfbc9501bb7 (diff)
downloadmirror-ca3a097505c42c2aec16e208c571c1b496c4b14b.tar.xz
Support reload and refactor config
- Simplify config parameters. - Support hot config reloading.
Diffstat (limited to 'internal/service/config.go')
-rw-r--r--internal/service/config.go59
1 files changed, 31 insertions, 28 deletions
diff --git a/internal/service/config.go b/internal/service/config.go
index bfe271e..0f34997 100644
--- a/internal/service/config.go
+++ b/internal/service/config.go
@@ -3,7 +3,6 @@ package service
import (
"fmt"
"os"
- "path"
"path/filepath"
"time"
@@ -11,28 +10,6 @@ import (
"github.com/BurntSushi/toml"
)
-type Duration struct {
- time.Duration
-}
-
-func DurationRef(v time.Duration) *Duration {
- return &Duration{v}
-}
-
-func (s Duration) MarshalText() ([]byte, error) {
- return []byte(s.Duration.String()), nil
-}
-
-func (s *Duration) UnmarshalText(text []byte) error {
- v, err := time.ParseDuration(string(text))
- if err != nil {
- return err
- }
- *s = Duration{v}
-
- return nil
-}
-
// Global parameters
type GlobalConfig struct {
MaxInterval *Duration `toml:"max-interval"`
@@ -56,7 +33,7 @@ var DefaultConfig = Config{
}
// Read the given configuration file.
-func ReadConfig(fp string) (*Config, error) {
+func readConfigFile(fp string) (*Config, error) {
var config Config
f, err := os.Open(fp)
@@ -74,11 +51,10 @@ func ReadConfig(fp string) (*Config, error) {
}
// Read all configuration in the given directory.
-func ReadConfigDir(fp string) (*Config, error) {
+func readConfigDir(fp string) (*Config, error) {
var cfg Config
- confDPath := path.Join(path.Join(fp, "conf.d"))
- confDDir, err := os.ReadDir(confDPath)
+ confDDir, err := os.ReadDir(fp)
if os.IsNotExist(err) {
// No directory is an empty one.
return &Config{}, nil
@@ -91,7 +67,7 @@ func ReadConfigDir(fp string) (*Config, error) {
continue
}
- entryCfg, err := ReadConfig(filepath.Join(confDPath, entry.Name()))
+ entryCfg, err := readConfigFile(filepath.Join(fp, entry.Name()))
if err != nil {
return nil, err
}
@@ -102,6 +78,33 @@ func ReadConfigDir(fp string) (*Config, error) {
return &cfg, nil
}
+// Read all configuration in the given directory.
+func ReadConfig(fps ...string) (*Config, error) {
+ var cfg Config
+
+ for _, fp := range fps {
+ stat, err := os.Stat(fp)
+ if err != nil {
+ return nil, err
+ }
+
+ var c *Config
+
+ if stat.IsDir() {
+ c, err = readConfigDir(fp)
+ if err != nil {
+ return nil, err
+ }
+ } else {
+ c, err = readConfigFile(fp)
+ }
+
+ cfg.Append(c)
+ }
+
+ return &cfg, nil
+}
+
// Apply the given configuration parameters.
func (c *Config) Append(src *Config) {
if src.MaxInterval != nil {