61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package pf
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
WebHost string
|
|
WebPort int
|
|
PublicBasePath string
|
|
DatabaseURL string
|
|
ScrapeIntervalHours int
|
|
TGBotToken string
|
|
TGBotUsername string
|
|
InternalAPIKey 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", ""), "@"),
|
|
InternalAPIKey: env("INTERNAL_API_KEY", env("PORTAL_INTERNAL_API_KEY", "")),
|
|
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
|
|
}
|