2023-12-15 16:35:41 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-12-14 12:09:01 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
2024-03-21 15:39:31 +01:00
|
|
|
"github.com/wneessen/logranger"
|
2023-12-14 12:09:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// LogErrKey is the keyword used in slog for error messages
|
|
|
|
LogErrKey = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-03-21 20:22:33 +01:00
|
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)).With(slog.String("context", "logranger"))
|
|
|
|
confPath := "logranger.toml"
|
|
|
|
confPathEnv := os.Getenv("LOGRANGER_CONFIG")
|
|
|
|
if confPathEnv != "" {
|
|
|
|
confPath = confPathEnv
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 20:22:33 +01:00
|
|
|
path := filepath.Dir(confPath)
|
|
|
|
file := filepath.Base(confPath)
|
|
|
|
config, err := logranger.NewConfig(path, file)
|
2023-12-14 12:09:01 +01:00
|
|
|
if err != nil {
|
2024-03-21 20:22:33 +01:00
|
|
|
logger.Error("failed to read/parse config", LogErrKey, err)
|
2023-12-14 12:09:01 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-03-21 20:22:33 +01:00
|
|
|
server, err := logranger.New(config)
|
2023-12-23 20:29:38 +01:00
|
|
|
if err != nil {
|
2024-03-21 20:22:33 +01:00
|
|
|
logger.Error("failed to create new server", LogErrKey, err)
|
2023-12-23 20:29:38 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:09:01 +01:00
|
|
|
go func() {
|
2024-03-21 20:22:33 +01:00
|
|
|
if err = server.Run(); err != nil {
|
|
|
|
logger.Error("failed to start logranger", LogErrKey, err)
|
2023-12-14 12:09:01 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-03-21 20:22:33 +01:00
|
|
|
signalChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalChan)
|
|
|
|
for recvSig := range signalChan {
|
|
|
|
if recvSig == syscall.SIGKILL || recvSig == syscall.SIGABRT || recvSig == syscall.SIGINT || recvSig == syscall.SIGTERM {
|
|
|
|
logger.Warn("received signal. shutting down server", slog.String("signal", recvSig.String()))
|
|
|
|
// server.Stop()
|
|
|
|
logger.Info("server gracefully shut down")
|
2023-12-15 17:35:27 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2024-03-21 20:22:33 +01:00
|
|
|
if recvSig == syscall.SIGHUP {
|
|
|
|
logger.Info(`received signal`,
|
2023-12-27 19:59:49 +01:00
|
|
|
slog.String("signal", "SIGHUP"),
|
|
|
|
slog.String("action", "reloading config/ruleset"))
|
2024-03-21 20:22:33 +01:00
|
|
|
if err = server.ReloadConfig(path, file); err != nil {
|
|
|
|
logger.Error("failed to reload config", LogErrKey, err)
|
2023-12-27 19:59:49 +01:00
|
|
|
}
|
2023-12-14 12:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|