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.
This commit is contained in:
Winni Neessen 2023-12-27 19:59:49 +01:00
parent fa527345a3
commit dd8d660571
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 25 additions and 14 deletions

View file

@ -58,20 +58,12 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if rc == syscall.SIGHUP { if rc == syscall.SIGHUP {
l.Info(`received "SIGHUP" signal - reloading rules...`) l.Info(`received signal`,
/* slog.String("signal", "SIGHUP"),
_, nr, err := config.New(config.WithConfFile(*cf), config.WithRulesFile(*rf)) slog.String("action", "reloading config/ruleset"))
if err != nil { if err = s.ReloadConfig(p, f); err != nil {
s.Log.Errorf("%s - skipping reload", err) l.Error("failed to reload config", LogErrKey, 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)
*/
} }
} }
} }

View file

@ -255,3 +255,22 @@ func (s *Server) setRules() error {
s.ruleset = rs s.ruleset = rs
return nil 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
}