buhapp-backend/internal/config/config.go
ga 8d754f9834 Sprint 1: scaffold backend (Go + Fiber + PG + Redis + MinIO)
- POST /api/v1/auth/register with mandatory consents
- POST /api/v1/auth/login
- GET  /api/v1/me (protected)
- GET  /api/v1/legal/{terms,privacy,disclaimer}
- Migrations for users, consent_log, audit_log
- bcrypt + JWT (access + refresh)
- Docker Compose stack
2026-08-20 15:41:05 +00:00

121 lines
2.9 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
"time"
"github.com/joho/godotenv"
)
type Config struct {
AppEnv string
AppPort int
AppURL string
PostgresHost string
PostgresPort int
PostgresUser string
PostgresPassword string
PostgresDB string
PostgresSSLMode string
RedisHost string
RedisPort int
RedisPassword string
RedisDB int
MinIOEndpoint string
MinIOAccessKey string
MinIOSecretKey string
MinIOBucket string
MinIOUseSSL bool
JWTSecret string
JWTAccessTTL time.Duration
JWTRefreshTTL time.Duration
TermsVersion string
PrivacyVersion string
DisclaimerVersion string
}
func Load() (*Config, error) {
_ = godotenv.Load()
cfg := &Config{
AppEnv: getEnv("APP_ENV", "development"),
AppPort: getEnvInt("APP_PORT", 8080),
AppURL: getEnv("APP_URL", "http://localhost:8080"),
PostgresHost: getEnv("POSTGRES_HOST", "localhost"),
PostgresPort: getEnvInt("POSTGRES_PORT", 5432),
PostgresUser: getEnv("POSTGRES_USER", "buhapp"),
PostgresPassword: getEnv("POSTGRES_PASSWORD", "buhapp_secret"),
PostgresDB: getEnv("POSTGRES_DB", "buhapp"),
PostgresSSLMode: getEnv("POSTGRES_SSLMODE", "disable"),
RedisHost: getEnv("REDIS_HOST", "localhost"),
RedisPort: getEnvInt("REDIS_PORT", 6379),
RedisPassword: getEnv("REDIS_PASSWORD", ""),
RedisDB: getEnvInt("REDIS_DB", 0),
MinIOEndpoint: getEnv("MINIO_ENDPOINT", "localhost:9000"),
MinIOAccessKey: getEnv("MINIO_ACCESS_KEY", "buhapp_minio"),
MinIOSecretKey: getEnv("MINIO_SECRET_KEY", "buhapp_minio_secret"),
MinIOBucket: getEnv("MINIO_BUCKET", "buhapp"),
MinIOUseSSL: getEnvBool("MINIO_USE_SSL", false),
JWTSecret: getEnv("JWT_SECRET", "change-me"),
JWTAccessTTL: time.Duration(getEnvInt("JWT_ACCESS_TTL", 3600)) * time.Second,
JWTRefreshTTL: time.Duration(getEnvInt("JWT_REFRESH_TTL", 2592000)) * time.Second,
TermsVersion: getEnv("TERMS_VERSION", "1.0"),
PrivacyVersion: getEnv("PRIVACY_VERSION", "1.0"),
DisclaimerVersion: getEnv("DISCLAIMER_VERSION", "1.0"),
}
if cfg.JWTSecret == "change-me" {
return nil, fmt.Errorf("JWT_SECRET must be set")
}
return cfg, nil
}
func (c *Config) PostgresDSN() string {
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
c.PostgresUser, c.PostgresPassword,
c.PostgresHost, c.PostgresPort,
c.PostgresDB, c.PostgresSSLMode)
}
func (c *Config) RedisAddr() string {
return fmt.Sprintf("%s:%d", c.RedisHost, c.RedisPort)
}
func getEnv(key, def string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return def
}
func getEnvInt(key string, def int) int {
if v, ok := os.LookupEnv(key); ok {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return def
}
func getEnvBool(key string, def bool) bool {
if v, ok := os.LookupEnv(key); ok {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return def
}