Интернет-магазин: Go, PostgreSQL 17 SSL, Caddy, Docker Compose
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user