newdrive/internal/config/config.go
Elijah f24e96efa7
All checks were successful
CI / Backend (push) Successful in 29s
CI / Frontend (push) Successful in 9m27s
CI / Contracts and repository policy (push) Successful in 7s
CI / Container (push) Successful in 18s
Harden CI and switch release builds to tagged immutable images
2026-07-15 19:21:07 -07:00

55 lines
1.2 KiB
Go

package config
import (
"fmt"
"log/slog"
"os"
"strings"
)
type Config struct {
HTTPAddress string
WebRoot string
StorageRoot string
PreviewCacheDir string
DatabaseURL string
LogLevel slog.Level
}
func FromEnvironment() (Config, error) {
level, err := parseLogLevel(environment("DRIVE_LOG_LEVEL", "info"))
if err != nil {
return Config{}, err
}
return Config{
HTTPAddress: environment("DRIVE_HTTP_ADDRESS", ":8080"),
WebRoot: environment("DRIVE_WEB_ROOT", "web/dist"),
StorageRoot: environment("DRIVE_STORAGE_ROOT", "/storage/.drive"),
PreviewCacheDir: environment("DRIVE_PREVIEW_CACHE_ROOT", "/cache/previews"),
DatabaseURL: os.Getenv("DRIVE_DATABASE_URL"),
LogLevel: level,
}, nil
}
func environment(name, fallback string) string {
if value := strings.TrimSpace(os.Getenv(name)); value != "" {
return value
}
return fallback
}
func parseLogLevel(value string) (slog.Level, error) {
switch strings.ToLower(value) {
case "debug":
return slog.LevelDebug, nil
case "info":
return slog.LevelInfo, nil
case "warn", "warning":
return slog.LevelWarn, nil
case "error":
return slog.LevelError, nil
default:
return 0, fmt.Errorf("invalid DRIVE_LOG_LEVEL %q", value)
}
}