load counter path from env

env
This commit is contained in:
Travis Shears 2025-10-11 22:19:13 +02:00
parent fb876226dd
commit be89141c06
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
2 changed files with 15 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"log/slog" "log/slog"
"os" "os"
"sync" "sync"
@ -18,7 +19,13 @@ type RequestCounter struct {
} }
// NewRequestCounter creates a new request counter with periodic snapshots // 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{ c := &RequestCounter{
counts: make(map[string]int64), counts: make(map[string]int64),
snapshotPath: snapshotPath, snapshotPath: snapshotPath,

View file

@ -141,7 +141,13 @@ func main() {
flag.Parse() flag.Parse()
// Initialize request counter with 30-second snapshot interval // 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 { if err != nil {
slog.Error("failed to initialize request counter", "error", err) slog.Error("failed to initialize request counter", "error", err)
os.Exit(1) os.Exit(1)