448cf2a465
Co-authored-by: Cursor <cursoragent@cursor.com>
49 lines
928 B
Go
49 lines
928 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
HTTPAddr string
|
|
DatabaseURL string
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
port := env("APP_PORT", "8080")
|
|
cfg := Config{
|
|
HTTPAddr: ":" + port,
|
|
DatabaseURL: os.Getenv("DATABASE_URL"),
|
|
ReadTimeout: durationEnv("HTTP_READ_TIMEOUT", 10*time.Second),
|
|
WriteTimeout: durationEnv("HTTP_WRITE_TIMEOUT", 30*time.Second),
|
|
}
|
|
if cfg.DatabaseURL == "" {
|
|
return cfg, fmt.Errorf("DATABASE_URL is required")
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func env(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func durationEnv(key string, fallback time.Duration) time.Duration {
|
|
v := os.Getenv(key)
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
sec, err := strconv.Atoi(v)
|
|
if err != nil || sec <= 0 {
|
|
return fallback
|
|
}
|
|
return time.Duration(sec) * time.Second
|
|
}
|