Inject Config as dependency for database, routing table, and snapshotter

- Remove old database Config struct and related functions
- Update database.New() to accept config.Config parameter
- Update routingtable.New() to accept config.Config parameter
- Update snapshotter.New() to accept config.Config parameter
- Simplify fx module providers in app.go
- Fix truthiness check for environment variables
- Handle empty state directory gracefully in routing table and snapshotter
- Update all tests to use empty state directory for testing
This commit is contained in:
2025-07-28 00:55:09 +02:00
parent 1a0622efaa
commit d15a5e91b9
7 changed files with 174 additions and 174 deletions

View File

@@ -11,6 +11,7 @@ import (
"sync"
"time"
"git.eeqj.de/sneak/routewatch/internal/config"
"git.eeqj.de/sneak/routewatch/internal/database"
"git.eeqj.de/sneak/routewatch/internal/metrics"
"git.eeqj.de/sneak/routewatch/internal/routingtable"
@@ -21,23 +22,11 @@ import (
"go.uber.org/fx"
)
// Config contains runtime configuration for RouteWatch
type Config struct {
MaxRuntime time.Duration // Maximum runtime (0 = run forever)
}
const (
// routingTableStatsInterval is how often we log routing table statistics
routingTableStatsInterval = 15 * time.Second
)
// NewConfig provides default configuration
func NewConfig() Config {
return Config{
MaxRuntime: 0, // Run forever by default
}
}
// Dependencies contains all dependencies for RouteWatch
type Dependencies struct {
fx.In
@@ -47,7 +36,7 @@ type Dependencies struct {
Streamer *streamer.Streamer
Server *server.Server
Logger *slog.Logger
Config Config `optional:"true"`
Config *config.Config
}
// RouteWatch represents the main application instance
@@ -63,6 +52,17 @@ type RouteWatch struct {
mu sync.Mutex
}
// isTruthy returns true if the value is considered truthy
// Empty string, "0", and "false" are considered falsy, everything else is truthy
func isTruthy(value string) bool {
return value != "" && value != "0" && value != "false"
}
// isSnapshotterEnabled checks if the snapshotter should be enabled based on environment variable
func isSnapshotterEnabled() bool {
return !isTruthy(os.Getenv("ROUTEWATCH_DISABLE_SNAPSHOTTER"))
}
// New creates a new RouteWatch instance
func New(deps Dependencies) *RouteWatch {
rw := &RouteWatch{
@@ -74,9 +74,9 @@ func New(deps Dependencies) *RouteWatch {
maxRuntime: deps.Config.MaxRuntime,
}
// Create snapshotter unless disabled (for tests)
if os.Getenv("ROUTEWATCH_DISABLE_SNAPSHOTTER") != "1" {
snap, err := snapshotter.New(deps.RoutingTable, deps.Logger)
// Create snapshotter if enabled
if isSnapshotterEnabled() {
snap, err := snapshotter.New(deps.RoutingTable, deps.Config, deps.Logger)
if err != nil {
deps.Logger.Error("Failed to create snapshotter", "error", err)
// Continue without snapshotter
@@ -235,13 +235,10 @@ func getModule() fx.Option {
return fx.Options(
fx.Provide(
NewLogger,
NewConfig,
config.New,
metrics.New,
database.New,
fx.Annotate(
func(db *database.Database) database.Store {
return db
},
database.New,
fx.As(new(database.Store)),
),
routingtable.New,

View File

@@ -7,6 +7,7 @@ import (
"testing"
"time"
"git.eeqj.de/sneak/routewatch/internal/config"
"git.eeqj.de/sneak/routewatch/internal/database"
"git.eeqj.de/sneak/routewatch/internal/metrics"
"git.eeqj.de/sneak/routewatch/internal/routingtable"
@@ -171,8 +172,14 @@ func TestRouteWatchLiveFeed(t *testing.T) {
// Create streamer
s := streamer.New(logger, metricsTracker)
// Create test config with empty state dir (no snapshot loading)
cfg := &config.Config{
StateDir: "",
MaxRuntime: 5 * time.Second,
}
// Create routing table
rt := routingtable.New(logger)
rt := routingtable.New(cfg, logger)
// Create server
srv := server.New(mockDB, rt, s, logger)
@@ -184,9 +191,7 @@ func TestRouteWatchLiveFeed(t *testing.T) {
Streamer: s,
Server: srv,
Logger: logger,
Config: Config{
MaxRuntime: 5 * time.Second,
},
Config: cfg,
}
rw := New(deps)