Files
mtusi2026lab7/backend/internal/adapter/lifecycle/environment.go

25 lines
618 B
Go

package lifecycle
import (
"backend/internal/adapter/freshness"
"os"
"strconv"
"time"
)
func FromEnvironment() (*freshness.Watcher, error) {
path := os.Getenv("SECRET_FRESHNESS_FILE")
if path == "" {
return nil, nil
}
age := 120
if s := os.Getenv("SECRET_MAX_AGE_SECONDS"); s != "" {
n, e := strconv.Atoi(s)
if e != nil || n < 1 || n > 3600 {
return nil, ErrConfiguration
}
age = n
}
return freshness.New(freshness.Ports{Read: freshness.FileReader(path), Now: time.Now, NewTicker: freshness.SystemTicker}, time.Duration(age)*time.Second, min(5*time.Second, time.Duration(age)*time.Second))
}