Skip to content

Commit

Permalink
all config in toml config file (#5)
Browse files Browse the repository at this point in the history
## πŸ“ Summary

Make config file support all config values.

---

## βœ… I have run these commands

* [x] `make lint`
* [x] `make test`
* [x] `go mod tidy`
  • Loading branch information
metachris authored Nov 14, 2024
1 parent 6f8b89a commit 90706a5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
62 changes: 33 additions & 29 deletions cmd/system-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
Expand All @@ -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: "",
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions systemapi-config.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 17 additions & 0 deletions systemapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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),
}
}
25 changes: 11 additions & 14 deletions systemapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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")
Expand Down

0 comments on commit 90706a5

Please sign in to comment.