package config import ( "fmt" "net" "os" "strconv" "strings" "time" ) type Config struct { Host string Port string Env string SessionTTL time.Duration DatabaseURL string CacheURL string S3Endpoint string S3Region string S3Bucket string S3AccessKeyID string S3SecretAccessKey string MaxUploadBytes int64 MaxImagePixels int64 MaxConcurrentUploads int MaxConcurrentDecodes int } func Load() (Config, error) { ttl, err := strconv.Atoi(getenv("SESSION_TTL_SECONDS", "3600")) if err != nil || ttl <= 0 { return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer") } maxUploadBytes, err := positiveInt64("MAX_UPLOAD_BYTES", "10485760") if err != nil { return Config{}, err } maxImagePixels, err := positiveInt64("MAX_IMAGE_PIXELS", "40000000") if err != nil { return Config{}, err } maxConcurrentUploads, err := positiveInt("MAX_CONCURRENT_UPLOADS", "8") if err != nil { return Config{}, err } maxConcurrentDecodes, err := positiveInt("MAX_CONCURRENT_DECODES", "1") if err != nil { return Config{}, err } databaseURL, err := ReadSecret("DATABASE_URL_FILE", "/run/secrets/database_url") if err != nil { return Config{}, err } cacheURL, err := ReadSecret("CACHE_URL_FILE", "/run/secrets/cache_url") if err != nil { return Config{}, err } accessKeyID, err := ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id") if err != nil { return Config{}, err } secretAccessKey, err := ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key") if err != nil { return Config{}, err } if accessKeyID == "" || secretAccessKey == "" { return Config{}, fmt.Errorf("S3_ACCESS_KEY_ID_FILE and S3_SECRET_ACCESS_KEY_FILE must not be empty") } bucket := getenv("S3_BUCKET", "messenger-attachments") if strings.TrimSpace(bucket) == "" { return Config{}, fmt.Errorf("S3_BUCKET must not be empty") } endpoint := getenv("S3_ENDPOINT", "http://s3:9000") if strings.TrimSpace(endpoint) == "" { return Config{}, fmt.Errorf("S3_ENDPOINT must not be empty") } return Config{ Host: getenv("APP_HOST", "0.0.0.0"), Port: getenv("APP_PORT", "8080"), Env: getenv("APP_ENV", "lab"), SessionTTL: time.Duration(ttl) * time.Second, DatabaseURL: databaseURL, CacheURL: cacheURL, S3Endpoint: endpoint, S3Region: getenv("S3_REGION", "us-east-1"), S3Bucket: bucket, S3AccessKeyID: accessKeyID, S3SecretAccessKey: secretAccessKey, MaxUploadBytes: maxUploadBytes, MaxImagePixels: maxImagePixels, MaxConcurrentUploads: maxConcurrentUploads, MaxConcurrentDecodes: maxConcurrentDecodes, }, nil } func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) } func positiveInt64(key, def string) (int64, error) { value, err := strconv.ParseInt(getenv(key, def), 10, 64) if err != nil || value <= 0 { return 0, fmt.Errorf("%s must be a positive integer", key) } return value, nil } func positiveInt(key, def string) (int, error) { value, err := strconv.Atoi(getenv(key, def)) if err != nil || value <= 0 { return 0, fmt.Errorf("%s must be a positive integer", key) } return value, nil } func ReadSecret(key, def string) (string, error) { path := getenv(key, def) b, err := os.ReadFile(path) if err != nil { return "", fmt.Errorf("%s: cannot read %s", key, path) } return strings.TrimSpace(string(b)), nil } func getenv(key, def string) string { if v := os.Getenv(key); v != "" { return v } return def }