diff --git a/counter.go b/counter.go index fa432c3..057c12a 100644 --- a/counter.go +++ b/counter.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "log/slog" "os" "sync" @@ -18,7 +19,13 @@ type RequestCounter struct { } // NewRequestCounter creates a new request counter with periodic snapshots -func NewRequestCounter(snapshotPath string, snapshotInterval time.Duration) (*RequestCounter, error) { +func NewRequestCounter(snapshotInterval time.Duration) (*RequestCounter, error) { + snapshotPath := os.Getenv("REQUEST_COUNTS_PATH") + if snapshotPath == "" { + err := errors.New("REQUEST_COUNTS_PATH environment variable must be set and non-empty") + slog.Error("failed to initialize request counter", "error", err) + os.Exit(1) + } c := &RequestCounter{ counts: make(map[string]int64), snapshotPath: snapshotPath, diff --git a/main.go b/main.go index 5dad5fc..fcca8ac 100644 --- a/main.go +++ b/main.go @@ -141,7 +141,13 @@ func main() { flag.Parse() // Initialize request counter with 30-second snapshot interval - counter, err := NewRequestCounter("request_counts.json", 30*time.Second) + requestCountsPath := os.Getenv("REQUEST_COUNTS_PATH") + if requestCountsPath == "" { + err = errors.New("REQUEST_COUNTS_PATH environment variable must be set and non-empty") + slog.Error("failed to initialize request counter", "error", err) + os.Exit(1) + } + counter, err := NewRequestCounter(30 * time.Second) if err != nil { slog.Error("failed to initialize request counter", "error", err) os.Exit(1)