From dd8d66057155188094992b424b5d0e0de7eccb7c Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 27 Dec 2023 19:59:49 +0100 Subject: [PATCH] Implemented ReloadConfig method in server A new ReloadConfig function has been added to the Server in server.go which reloads server configurations and updates the Server's conf field as well as the configured Ruleset. Changes have also been made to the handling of the SIGHUP signal in main.go, using this new function to reload configurations and updating the logs accordingly. --- cmd/server/main.go | 20 ++++++-------------- server.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index cf0eece..6f99ff3 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -58,20 +58,12 @@ func main() { os.Exit(0) } if rc == syscall.SIGHUP { - l.Info(`received "SIGHUP" signal - reloading rules...`) - /* - _, nr, err := config.New(config.WithConfFile(*cf), config.WithRulesFile(*rf)) - if err != nil { - s.Log.Errorf("%s - skipping reload", err) - continue - } - if err := nr.CheckRegEx(); err != nil { - s.Log.Errorf("ruleset validation failed for new ruleset - skipping reload: %s", err) - continue - } - s.SetRules(nr) - - */ + l.Info(`received signal`, + slog.String("signal", "SIGHUP"), + slog.String("action", "reloading config/ruleset")) + if err = s.ReloadConfig(p, f); err != nil { + l.Error("failed to reload config", LogErrKey, err) + } } } } diff --git a/server.go b/server.go index b2f406f..f6ed071 100644 --- a/server.go +++ b/server.go @@ -255,3 +255,22 @@ func (s *Server) setRules() error { s.ruleset = rs return nil } + +// ReloadConfig reloads the configuration of the Server with the specified +// path and filename. +// It creates a new Config using the NewConfig method and updates the Server's +// conf field. It also reloads the configured Ruleset. +// If an error occurs while reloading the configuration, an error is returned. +func (s *Server) ReloadConfig(p, f string) error { + c, err := NewConfig(p, f) + if err != nil { + return fmt.Errorf("failed to reload config: %w", err) + } + s.conf = c + + if err := s.setRules(); err != nil { + return fmt.Errorf("failed to reload ruleset: %w", err) + } + + return nil +}