config.go
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type Config struct { |
| 12 | Port int |
| 13 | DataDir string |
| 14 | DBPath string |
| 15 | LibraryDir string |
| 16 | TempDir string |
| 17 | YTDLPPath string |
| 18 | BaseURL string |
| 19 | Workers int |
| 20 | RefreshInterval int |
| 21 | SchedulerInterval int |
| 22 | } |
| 23 | |
| 24 | func New() *Config { |
| 25 | dataDir := getEnv("VIDARCHIVE_DATA_DIR", "./data") |
| 26 | |
| 27 | return &Config{ |
| 28 | Port: getEnvInt("VIDARCHIVE_PORT", 8080), |
| 29 | DataDir: dataDir, |
| 30 | DBPath: getEnv("VIDARCHIVE_DB_PATH", filepath.Join(dataDir, "vidarchive.db")), |
| 31 | LibraryDir: getEnv("VIDARCHIVE_LIBRARY_DIR", filepath.Join(dataDir, "library")), |
| 32 | TempDir: getEnv("VIDARCHIVE_TEMP_DIR", filepath.Join(dataDir, "temp")), |
| 33 | YTDLPPath: getEnv("VIDARCHIVE_YTDLP_PATH", "yt-dlp"), |
| 34 | BaseURL: getEnv("VIDARCHIVE_BASE_URL", ""), |
| 35 | Workers: getEnvInt("VIDARCHIVE_WORKERS", 2), |
| 36 | RefreshInterval: getEnvInt("VIDARCHIVE_REFRESH_INTERVAL", 5), |
| 37 | SchedulerInterval: getEnvInt("VIDARCHIVE_SCHEDULER_INTERVAL", 60), |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func (c *Config) IsHTTPS() bool { |
| 42 | return strings.HasPrefix(c.BaseURL, "https://") |
| 43 | } |
| 44 | |
| 45 | func getEnv(key, defaultVal string) string { |
| 46 | if v := os.Getenv(key); v != "" { |
| 47 | return v |
| 48 | } |
| 49 | return defaultVal |
| 50 | } |
| 51 | |
| 52 | func getEnvInt(key string, defaultVal int) int { |
| 53 | v := os.Getenv(key) |
| 54 | if v == "" { |
| 55 | return defaultVal |
| 56 | } |
| 57 | i, err := strconv.Atoi(v) |
| 58 | if err != nil { |
| 59 | fmt.Fprintf(os.Stderr, "invalid %s: %v, using default %d\n", key, err, defaultVal) |
| 60 | return defaultVal |
| 61 | } |
| 62 | return i |
| 63 | } |
| 64 |