Files

66 lines
1.5 KiB
Go
Raw Permalink Normal View History

2026-07-07 10:29:28 +08:00
package config
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
ServerPort string
JWTSecret string
JWTExpiryHrs int
DatabaseURL string
ArtefactDir string
MaxConcurrent int
BandwidthBPS int
RateLimitRPS int
ServeSPA bool
2026-07-07 10:29:28 +08:00
}
func Load() *Config {
// Load .env file if present (silently ignore if not found)
if err := godotenv.Load(); err != nil {
log.Println("[config] No .env file found, using system environment variables")
}
return &Config{
ServerPort: envOrDefault("SERVER_PORT", "8080"),
JWTSecret: envOrDefault("JWT_SECRET", "change-me-in-production"),
JWTExpiryHrs: envOrDefaultInt("JWT_EXPIRY_HRS", 24),
DatabaseURL: envOrDefault("DATABASE_URL", "postgres://postgres:postgres@localhost:5432/manifest?sslmode=disable"),
ArtefactDir: envOrDefault("ARTEFACT_DIR", "/var/ota/artefacts"),
MaxConcurrent: envOrDefaultInt("MAX_CONCURRENT_DOWNLOADS", 50),
BandwidthBPS: envOrDefaultInt("BANDWIDTH_BPS", 10*1024*1024/50),
RateLimitRPS: envOrDefaultInt("RATE_LIMIT_RPS", 100),
ServeSPA: envOrDefaultBool("SERVE_SPA", true),
2026-07-07 10:29:28 +08:00
}
}
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func envOrDefaultInt(key string, def int) int {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return def
}
func envOrDefaultBool(key string, def bool) bool {
if v := os.Getenv(key); v != "" {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return def
}