Move monitoring PF infrastructure to Go
This commit is contained in:
58
internal/pf/config.go
Normal file
58
internal/pf/config.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package pf
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
WebHost string
|
||||
WebPort int
|
||||
PublicBasePath string
|
||||
DatabaseURL string
|
||||
ScrapeIntervalHours int
|
||||
TGBotToken string
|
||||
TGBotUsername string
|
||||
WorkerPython string
|
||||
WorkerModule string
|
||||
}
|
||||
|
||||
func LoadConfig() Config {
|
||||
return Config{
|
||||
WebHost: env("WEB_HOST", "127.0.0.1"),
|
||||
WebPort: envInt("WEB_PORT", 8000),
|
||||
PublicBasePath: strings.TrimRight(env("PUBLIC_BASE_PATH", ""), "/"),
|
||||
DatabaseURL: env("DATABASE_URL", "sqlite:///data/monitor.db"),
|
||||
ScrapeIntervalHours: max(1, envInt("SCRAPE_INTERVAL_HOURS", 4)),
|
||||
TGBotToken: env("TG_BOT_TOKEN", ""),
|
||||
TGBotUsername: strings.TrimPrefix(env("TG_BOT_USERNAME", ""), "@"),
|
||||
WorkerPython: env("WORKER_PYTHON", "python"),
|
||||
WorkerModule: env("WORKER_MODULE", "app.worker"),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) SchedulerInterval() time.Duration {
|
||||
return time.Duration(c.ScrapeIntervalHours) * time.Hour
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
value := strings.TrimSpace(os.Getenv(key))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func envInt(key string, fallback int) int {
|
||||
raw := strings.TrimSpace(os.Getenv(key))
|
||||
if raw == "" {
|
||||
return fallback
|
||||
}
|
||||
value, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user