aboutsummaryrefslogtreecommitdiffsponsor
path: root/cmd
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 /cmd
parentc8ba7263e7923fc76eee7dbe129a8bfbc9501bb7 (diff)
downloadmirror-ca3a097505c42c2aec16e208c571c1b496c4b14b.tar.xz
Support reload and refactor config
- Simplify config parameters. - Support hot config reloading.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/mirror/main.go46
1 files changed, 26 insertions, 20 deletions
diff --git a/cmd/mirror/main.go b/cmd/mirror/main.go
index 1da9b15..4c551f0 100644
--- a/cmd/mirror/main.go
+++ b/cmd/mirror/main.go
@@ -6,6 +6,7 @@ import (
"log"
"os"
"os/signal"
+ "strings"
"syscall"
"git.server.ky/slackcoder/mirror/internal/service"
@@ -23,8 +24,7 @@ type Flags struct {
func ParseFlags() *Flags {
var flags Flags
- flag.StringVar(&flags.Config, "config", "/etc/mirror/mirror.toml", "configuration file which takes precedence")
- flag.StringVar(&flags.ConfigDir, "config-dir", "/etc/mirror/conf.d", "configuration directory")
+ flag.StringVar(&flags.Config, "config", "/etc/mirror/conf.d,/etc/mirror/mirror.toml", "comma separated configuration files or directories")
flag.BoolVar(&flags.Version, "version", false, "print version")
flag.Parse()
@@ -38,11 +38,27 @@ func exitOnError(err error) {
}
}
-func handleSignal(c <-chan os.Signal, srv *service.Service) {
+func splitCommaString(str string) []string {
+ strs := strings.Split(str, ",")
+
+ for i := range strs {
+ strs[i] = strings.TrimSpace(strs[i])
+ }
+
+ return strs
+}
+
+func handleSignal(c <-chan os.Signal, srv *service.Service, flags *Flags) {
for v := range c {
if v == syscall.SIGHUP {
- var cfg service.Config
- _ = srv.Reload(&cfg)
+ configs := splitCommaString(flags.Config)
+ cfg, err := service.ReadConfig(configs...)
+ if err != nil {
+ srv.Log(err.Error())
+ continue
+ }
+
+ srv.Reload(cfg)
}
}
}
@@ -56,26 +72,16 @@ func main() {
os.Exit(0)
}
- signalCh := make(chan os.Signal, 1)
- signal.Notify(signalCh, syscall.SIGHUP)
-
- var cfg service.Config
-
- c, err := service.ReadConfigDir(flags.ConfigDir)
+ configs := splitCommaString(flags.Config)
+ cfg, err := service.ReadConfig(configs...)
exitOnError(err)
- cfg.Append(c)
-
- c, err = service.ReadConfig(flags.Config)
+ srv, err := service.NewService(cfg)
exitOnError(err)
- cfg.Append(c)
-
- srv, err := service.NewService(&cfg)
- exitOnError(err)
-
- signalCh = make(chan os.Signal, 1)
+ signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGHUP)
+ go handleSignal(signalCh, srv, flags)
err = srv.Run()
exitOnError(err)