saveinmed/backend-go/internal/config/config.go

63 lines
1.5 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
"time"
)
// Config centralizes runtime configuration loaded from the environment.
type Config struct {
AppName string
Port string
DatabaseURL string
MaxOpenConns int
MaxIdleConns int
ConnMaxIdle time.Duration
}
// Load reads configuration from environment variables and applies sane defaults
// for local development.
func Load() Config {
cfg := Config{
AppName: getEnv("APP_NAME", "saveinmed-performance-core"),
Port: getEnv("PORT", "8080"),
DatabaseURL: getEnv("DATABASE_URL", "postgres://postgres:postgres@localhost:5432/saveinmed?sslmode=disable"),
MaxOpenConns: getEnvInt("DB_MAX_OPEN_CONNS", 15),
MaxIdleConns: getEnvInt("DB_MAX_IDLE_CONNS", 5),
ConnMaxIdle: getEnvDuration("DB_CONN_MAX_IDLE", 5*time.Minute),
}
return cfg
}
// Addr returns the address binding for the HTTP server.
func (c Config) Addr() string {
return fmt.Sprintf(":%s", c.Port)
}
func getEnv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if value := os.Getenv(key); value != "" {
if parsed, err := strconv.Atoi(value); err == nil {
return parsed
}
}
return fallback
}
func getEnvDuration(key string, fallback time.Duration) time.Duration {
if value := os.Getenv(key); value != "" {
if parsed, err := time.ParseDuration(value); err == nil {
return parsed
}
}
return fallback
}