From be89141c062e7ce77957514231b8268a6ffe2b35 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sat, 11 Oct 2025 22:19:13 +0200 Subject: [PATCH] load counter path from env env --- counter.go | 9 ++++++++- main.go | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) 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)