From 90706a5d560158f5ee51a70f9212d393bee10518 Mon Sep 17 00:00:00 2001 From: Chris Hager Date: Thu, 14 Nov 2024 15:38:33 +0100 Subject: [PATCH] all config in toml config file (#5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📝 Summary Make config file support all config values. --- ## ✅ I have run these commands * [x] `make lint` * [x] `make test` * [x] `go mod tidy` --- cmd/system-api/main.go | 62 ++++++++++++++++++++++-------------------- systemapi-config.toml | 6 ++++ systemapi/config.go | 17 ++++++++++++ systemapi/server.go | 25 ++++++++--------- 4 files changed, 67 insertions(+), 43 deletions(-) diff --git a/cmd/system-api/main.go b/cmd/system-api/main.go index b05e8fa..d32242c 100644 --- a/cmd/system-api/main.go +++ b/cmd/system-api/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "log" "os" "os/signal" @@ -24,16 +25,6 @@ var flags []cli.Flag = []cli.Flag{ Value: "pipe.fifo", Usage: "filename for named pipe (for sending events into this service)", }, - &cli.BoolFlag{ - Name: "log-json", - Value: false, - Usage: "log in JSON format", - }, - &cli.BoolFlag{ - Name: "log-debug", - Value: true, - Usage: "log debug messages", - }, &cli.StringFlag{ Name: "config", Value: "", @@ -56,40 +47,53 @@ func main() { } func runCli(cCtx *cli.Context) (err error) { + // Load cli arguments + configFile := cCtx.String("config") listenAddr := cCtx.String("listen-addr") pipeFile := cCtx.String("pipe-file") - logJSON := cCtx.Bool("log-json") - logDebug := cCtx.Bool("log-debug") + // Create configuration file (load from file if requested) + config := systemapi.NewSystemAPIConfig() + if configFile != "" { + config, err = systemapi.LoadConfigFromFile(configFile) + if err != nil { + fmt.Println("Error loading config", err) + return err + } + } + + // Override configuration with cli arguments, if present + if listenAddr == "" { + config.General.ListenAddr = listenAddr + } + if pipeFile == "" { + config.General.PipeFile = pipeFile + } + + // Setup logging logTags := map[string]string{ "version": common.Version, } - configFile := cCtx.String("config") - log := common.SetupLogger(&common.LoggingOpts{ - JSON: logJSON, - Debug: logDebug, + JSON: config.General.LogJSON, + Debug: config.General.LogDebug, Concise: true, RequestHeaders: true, Tags: logTags, }) - var config *systemapi.SystemAPIConfig - if configFile != "" { - config, err = systemapi.LoadConfigFromFile(configFile) - if err != nil { - log.Error("Error loading config", "err", err) - return err - } - log.Info("Loaded config", "config-file", config) - } + // Print configuration + log.Info("config:", + "listenAddr", config.General.ListenAddr, + "pipeFile", config.General.PipeFile, + "logJSON", config.General.LogJSON, + "logDebug", config.General.LogDebug, + ) // Setup and start the server (in the background) cfg := &systemapi.HTTPServerConfig{ - ListenAddr: listenAddr, - Log: log, - PipeFilename: pipeFile, - Config: config, + Log: log, + Config: config, } server, err := systemapi.NewServer(cfg) if err != nil { diff --git a/systemapi-config.toml b/systemapi-config.toml index 20952f0..21c7fb9 100644 --- a/systemapi-config.toml +++ b/systemapi-config.toml @@ -1,3 +1,9 @@ +[general] +listen_addr = "0.0.0.0:3535" +pipe_file = "pipe.fifo" +log_json = false +log_debug = true + [actions] # reboot = "reboot" # rbuilder_restart = "/etc/init.d/rbuilder restart" diff --git a/systemapi/config.go b/systemapi/config.go index 175a75e..c034135 100644 --- a/systemapi/config.go +++ b/systemapi/config.go @@ -6,7 +6,16 @@ import ( toml "github.com/pelletier/go-toml/v2" ) +type systemAPIConfigGeneral struct { + ListenAddr string `toml:"listen_addr"` + PipeFile string `toml:"pipe_file"` + LogJSON bool `toml:"log_json"` + LogDebug bool `toml:"log_debug"` +} + type SystemAPIConfig struct { + General systemAPIConfigGeneral + Actions map[string]string FileUploads map[string]string `toml:"file_uploads"` } @@ -27,3 +36,11 @@ func LoadConfig(content []byte) (*SystemAPIConfig, error) { } return cfg, nil } + +func NewSystemAPIConfig() *SystemAPIConfig { + return &SystemAPIConfig{ + General: systemAPIConfigGeneral{}, + Actions: make(map[string]string), + FileUploads: make(map[string]string), + } +} diff --git a/systemapi/server.go b/systemapi/server.go index b84d1d2..b3c47d7 100644 --- a/systemapi/server.go +++ b/systemapi/server.go @@ -22,17 +22,14 @@ import ( ) type HTTPServerConfig struct { - ListenAddr string - Log *httplog.Logger - PipeFilename string - EnablePprof bool + Config *SystemAPIConfig + Log *httplog.Logger + EnablePprof bool DrainDuration time.Duration GracefulShutdownDuration time.Duration ReadTimeout time.Duration WriteTimeout time.Duration - - Config *SystemAPIConfig } type Event struct { @@ -58,9 +55,9 @@ func NewServer(cfg *HTTPServerConfig) (srv *Server, err error) { events: make([]Event, 0), } - if cfg.PipeFilename != "" { - os.Remove(cfg.PipeFilename) - err := syscall.Mknod(cfg.PipeFilename, syscall.S_IFIFO|0o666, 0) + if cfg.Config.General.PipeFile != "" { + os.Remove(cfg.Config.General.PipeFile) + err := syscall.Mknod(cfg.Config.General.PipeFile, syscall.S_IFIFO|0o666, 0) if err != nil { return nil, err } @@ -69,7 +66,7 @@ func NewServer(cfg *HTTPServerConfig) (srv *Server, err error) { } srv.srv = &http.Server{ - Addr: cfg.ListenAddr, + Addr: cfg.Config.General.ListenAddr, Handler: srv.getRouter(), ReadTimeout: cfg.ReadTimeout, WriteTimeout: cfg.WriteTimeout, @@ -101,7 +98,7 @@ func (s *Server) getRouter() http.Handler { } func (s *Server) readPipeInBackground() { - file, err := os.OpenFile(s.cfg.PipeFilename, os.O_CREATE, os.ModeNamedPipe) + file, err := os.OpenFile(s.cfg.Config.General.PipeFile, os.O_CREATE, os.ModeNamedPipe) if err != nil { s.log.Error("Open named pipe file error:", "error", err) return @@ -123,7 +120,7 @@ func (s *Server) readPipeInBackground() { } func (s *Server) Start() { - s.log.Info("Starting HTTP server", "listenAddress", s.cfg.ListenAddr) + s.log.Info("Starting HTTP server", "listenAddress", s.cfg.Config.General.ListenAddr) if err := s.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { s.log.Error("HTTP server failed", "err", err) } @@ -136,8 +133,8 @@ func (s *Server) Shutdown(ctx context.Context) error { s.log.Error("HTTP server shutdown failed", "err", err) } - if s.cfg.PipeFilename != "" { - os.Remove(s.cfg.PipeFilename) + if s.cfg.Config.General.PipeFile != "" { + os.Remove(s.cfg.Config.General.PipeFile) } s.log.Info("HTTP server shutdown")