backend-service/internal/config/config.go

141 lines
3.0 KiB
Go
Raw Permalink Normal View History

package config
import (
"os"
"strconv"
"time"
)
type Config struct {
Server ServerConfig
Database DatabaseConfig
JWT JWTConfig
Stripe StripeConfig
SMTP SMTPConfig
Jitsi JitsiConfig
Logging LoggingConfig
}
type ServerConfig struct {
Port string
Host string
}
type DatabaseConfig struct {
Host string
Port string
User string
Password string
Name string
SSLMode string
}
type JWTConfig struct {
Secret string
Expiration time.Duration
}
type StripeConfig struct {
SecretKey string
WebhookSecret string
PublishableKey string
}
type SMTPConfig struct {
Host string
Port int
Username string
Password string
From string
}
type JitsiConfig struct {
BaseURL string
APIKey string
AppID string
PrivateKey string
}
type LoggingConfig struct {
Level string
Format string
Output string
MaxSize int
MaxBackups int
MaxAge int
}
func Load() (*Config, error) {
cfg := &Config{
Server: ServerConfig{
Port: getEnv("PORT", "8080"),
Host: getEnv("HOST", "localhost"),
},
Database: DatabaseConfig{
Host: getEnv("DB_HOST", "localhost"),
Port: getEnv("DB_PORT", "5432"),
User: getEnv("DB_USER", "postgres"),
Password: getEnv("DB_PASSWORD", ""),
Name: getEnv("DB_NAME", "booking_system"),
SSLMode: getEnv("DB_SSLMODE", "disable"),
},
JWT: JWTConfig{
Secret: getEnv("JWT_SECRET", "your-secret-key"),
Expiration: getEnvDuration("JWT_EXPIRATION", 24*time.Hour),
},
Stripe: StripeConfig{
SecretKey: getEnv("STRIPE_SECRET_KEY", ""),
WebhookSecret: getEnv("STRIPE_WEBHOOK_SECRET", ""),
PublishableKey: getEnv("STRIPE_PUBLISHABLE_KEY", ""),
},
SMTP: SMTPConfig{
Host: getEnv("SMTP_HOST", ""),
Port: getEnvInt("SMTP_PORT", 587),
Username: getEnv("SMTP_USERNAME", ""),
Password: getEnv("SMTP_PASSWORD", ""),
From: getEnv("SMTP_FROM", ""),
},
Jitsi: JitsiConfig{
BaseURL: getEnv("JITSI_BASE_URL", ""),
APIKey: getEnv("JITSI_API_KEY", ""),
AppID: getEnv("JITSI_APP_ID", ""),
PrivateKey: getEnv("JITSI_PRIVATE_KEY", ""),
},
Logging: LoggingConfig{
Level: getEnv("LOG_LEVEL", "INFO"),
Format: getEnv("LOG_FORMAT", "json"),
Output: getEnv("LOG_OUTPUT", "stdout"),
MaxSize: getEnvInt("LOG_MAX_SIZE", 100),
MaxBackups: getEnvInt("LOG_MAX_BACKUPS", 3),
MaxAge: getEnvInt("LOG_MAX_AGE", 28),
},
}
return cfg, nil
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func getEnvInt(key string, defaultValue int) int {
if value := os.Getenv(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
func getEnvDuration(key string, defaultValue time.Duration) time.Duration {
if value := os.Getenv(key); value != "" {
if duration, err := time.ParseDuration(value); err == nil {
return duration
}
}
return defaultValue
}