- Create internal/logger package with Logger wrapper around slog - Logger automatically adds source file, line number, and function name to all log entries - Use golang.org/x/term to properly detect if stdout is a terminal - Replace all slog.Logger usage with logger.Logger throughout the codebase - Remove verbose logging from database GetStats() method - Update all constructors and dependencies to use the new logger
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package routewatch
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.eeqj.de/sneak/routewatch/internal/logger"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
const (
|
|
// shutdownTimeout is the maximum time allowed for graceful shutdown
|
|
shutdownTimeout = 60 * time.Second
|
|
)
|
|
|
|
// CLIEntry is the main entry point for the CLI
|
|
func CLIEntry() {
|
|
app := fx.New(
|
|
getModule(),
|
|
fx.StopTimeout(shutdownTimeout), // Allow 60 seconds for graceful shutdown
|
|
fx.Invoke(func(lc fx.Lifecycle, rw *RouteWatch, logger *logger.Logger) {
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(_ context.Context) error {
|
|
go func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// Handle shutdown signals
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
<-sigCh
|
|
logger.Info("Received shutdown signal")
|
|
cancel()
|
|
}()
|
|
|
|
if err := rw.Run(ctx); err != nil {
|
|
logger.Error("RouteWatch error", "error", err)
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
},
|
|
OnStop: func(_ context.Context) error {
|
|
logger.Info("Shutting down RouteWatch")
|
|
rw.Shutdown()
|
|
|
|
return nil
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
|
|
app.Run()
|
|
}
|