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 | FFmpegPath string |
| 19 | FFprobePath string |
| 20 | BaseURL string |
| 21 | Workers int |
| 22 | RefreshInterval int |
| 23 | SchedulerInterval int |
| 24 | } |
| 25 | |
| 26 | func New() *Config { |
| 27 | dataDir := getEnv("VIDARCHIVE_DATA_DIR", "./data") |
| 28 | |
| 29 | return &Config{ |
| 30 | Port: getEnvInt("VIDARCHIVE_PORT", 8080), |
| 31 | DataDir: dataDir, |
| 32 | DBPath: getEnv("VIDARCHIVE_DB_PATH", filepath.Join(dataDir, "vidarchive.db")), |
| 33 | LibraryDir: getEnv("VIDARCHIVE_LIBRARY_DIR", filepath.Join(dataDir, "library")), |
| 34 | TempDir: getEnv("VIDARCHIVE_TEMP_DIR", filepath.Join(dataDir, "temp")), |
| 35 | YTDLPPath: getEnv("VIDARCHIVE_YTDLP_PATH", "yt-dlp"), |
| 36 | FFmpegPath: getEnv("VIDARCHIVE_FFMPEG_PATH", "ffmpeg"), |
| 37 | FFprobePath: getEnv("VIDARCHIVE_FFPROBE_PATH", "ffprobe"), |
| 38 | BaseURL: getEnv("VIDARCHIVE_BASE_URL", ""), |
| 39 | Workers: getEnvIntMin("VIDARCHIVE_WORKERS", 2, 1), |
| 40 | RefreshInterval: getEnvIntMin("VIDARCHIVE_REFRESH_INTERVAL", 5, 1), |
| 41 | SchedulerInterval: getEnvIntMin("VIDARCHIVE_SCHEDULER_INTERVAL", 60, 1), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (c *Config) IsHTTPS() bool { |
| 46 | return strings.HasPrefix(c.BaseURL, "https://") |
| 47 | } |
| 48 | |
| 49 | func getEnv(key, defaultVal string) string { |
| 50 | if v := os.Getenv(key); v != "" { |
| 51 | return v |
| 52 | } |
| 53 | return defaultVal |
| 54 | } |
| 55 | |
| 56 | func getEnvInt(key string, defaultVal int) int { |
| 57 | v := os.Getenv(key) |
| 58 | if v == "" { |
| 59 | return defaultVal |
| 60 | } |
| 61 | i, err := strconv.Atoi(v) |
| 62 | if err != nil { |
| 63 | fmt.Fprintf(os.Stderr, "invalid %s: %v, using default %d\n", key, err, defaultVal) |
| 64 | return defaultVal |
| 65 | } |
| 66 | return i |
| 67 | } |
| 68 | |
| 69 | // getEnvIntMin is getEnvInt with a lower bound. A value below min is rejected in |
| 70 | // favour of the default: a zero or negative worker count or tick interval would |
| 71 | // otherwise stall downloads or spin the scheduler. |
| 72 | func getEnvIntMin(key string, defaultVal, min int) int { |
| 73 | i := getEnvInt(key, defaultVal) |
| 74 | if i < min { |
| 75 | fmt.Fprintf(os.Stderr, "invalid %s: %d is below the minimum %d, using default %d\n", key, i, min, defaultVal) |
| 76 | return defaultVal |
| 77 | } |
| 78 | return i |
| 79 | } |
| 80 |