From c252c9a9341a06755790eb067e9597d706914cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20DELHAIE?= Date: Sun, 2 Nov 2025 14:35:20 +0100 Subject: [PATCH] add daemon config --- cmd/cli/config/config.go | 10 ++++- cmd/server/api/api.go | 4 +- cmd/server/core/config/config.go | 71 ++++++++++++++++++++++++++++++++ cmd/server/main.go | 25 +++++++++-- 4 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 cmd/server/core/config/config.go diff --git a/cmd/cli/config/config.go b/cmd/cli/config/config.go index 3d60e9f..90f6d42 100644 --- a/cmd/cli/config/config.go +++ b/cmd/cli/config/config.go @@ -40,7 +40,7 @@ func Load() ClientConfiguration { panic("failed to read the configuration file: " + err.Error()) } - return c + return fillDefault(c) } func Default() ClientConfiguration { @@ -50,3 +50,11 @@ func Default() ClientConfiguration { }, } } + +func fillDefault(c ClientConfiguration) ClientConfiguration { + if len(c.Deamon.URL) == 0 { + c.Deamon.URL = Default().Deamon.URL + } + + return c +} diff --git a/cmd/server/api/api.go b/cmd/server/api/api.go index 81e7f5f..d0fc31e 100644 --- a/cmd/server/api/api.go +++ b/cmd/server/api/api.go @@ -25,7 +25,7 @@ type ( } ) -func NewServer(data *storage.Repository, scheduler *cronruntime.Scheduler, port int) *HTTPServer { +func NewServer(data *storage.Repository, scheduler *cronruntime.Scheduler, addr string, port int) *HTTPServer { s := &HTTPServer{ data: data, scheduler: scheduler, @@ -57,7 +57,7 @@ func NewServer(data *storage.Repository, scheduler *cronruntime.Scheduler, port }) }) s.Server = &http.Server{ - Addr: fmt.Sprintf(":%d", port), + Addr: fmt.Sprintf("%s:%d", addr, port), Handler: router, } return s diff --git a/cmd/server/core/config/config.go b/cmd/server/core/config/config.go new file mode 100644 index 0000000..aad81c3 --- /dev/null +++ b/cmd/server/core/config/config.go @@ -0,0 +1,71 @@ +package config + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" +) + +type ( + DaemonConfiguration struct { + Server ServerConfiguration `json:"server"` + Database DatabaseConfiguration `json:"database"` + } + + ServerConfiguration struct { + Address string `json:"addr"` + Port uint16 `json:"port"` + } + + DatabaseConfiguration struct { + Path string `json:"path"` + } +) + +func Load(path string) (DaemonConfiguration, error) { + path = filepath.Join(path, "config.json") + + f, err := os.OpenFile(path, os.O_RDONLY, 0) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return Default(), nil + } + return DaemonConfiguration{}, fmt.Errorf("failed to load configuration file (%s): %s", path, err) + } + defer f.Close() + + var c DaemonConfiguration + d := json.NewDecoder(f) + if err := d.Decode(&c); err != nil { + return DaemonConfiguration{}, fmt.Errorf("failed to read configuration file (%s): %s", path, err) + } + + return fillDefault(c), nil +} + +func Default() DaemonConfiguration { + return DaemonConfiguration{ + Server: ServerConfiguration{ + Address: "127.0.0.1", + Port: 25697, + }, + Database: DatabaseConfiguration{ + Path: "/var/lib/mirror-sync/data.db", + }, + } +} + +func fillDefault(c DaemonConfiguration) DaemonConfiguration { + if len(c.Database.Path) == 0 { + c.Database.Path = Default().Database.Path + } + if len(c.Server.Address) == 0 { + c.Server.Address = Default().Server.Address + } + if c.Server.Port == 0 { + c.Server.Port = Default().Server.Port + } + return c +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 47114d7..6a628ce 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,9 +1,11 @@ package main import ( + "flag" "fmt" "log/slog" "mirror-sync/cmd/server/api" + "mirror-sync/cmd/server/core/config" cronruntime "mirror-sync/cmd/server/core/runtime" "mirror-sync/cmd/server/core/storage" "mirror-sync/pkg/constants" @@ -13,14 +15,29 @@ import ( ) func main() { + ecpath := os.Getenv("MIRRORSYNC_CONFIG_PATH") + if len(ecpath) == 0 { + ecpath = "/etc/mirror-sync" + } + + var configPath string + flag.StringVar(&configPath, "config", ecpath, "path to the configuration folder") + flag.Parse() + + c, err := config.Load(configPath) + if err != nil { + fmt.Fprintln(os.Stderr, "failed to start server:", err.Error()) + os.Exit(1) + } + dbPath := os.Getenv("MIRRORSYNC_DB_PATH") if len(dbPath) == 0 { - dbPath = "/var/lib/mirror-sync/data.db" + dbPath = c.Database.Path } p := os.Getenv("MIRRORSYNC_PORT") if len(p) == 0 { - p = "25697" + p = fmt.Sprintf("%d", c.Server.Port) } port, err := strconv.Atoi(p) @@ -59,9 +76,9 @@ func main() { slog.Info("daemon scheduler is running") // api - s := api.NewServer(data, scheduler, port) + s := api.NewServer(data, scheduler, c.Server.Address, port) - slog.Info("daemon listening to :" + p) + slog.Info(fmt.Sprintf("daemon listening to %s:%s", c.Server.Address, p)) if err := s.Server.ListenAndServe(); err != nil { fmt.Fprintln(os.Stderr, "failed to start server:", err.Error()) os.Exit(1)