Files

102 lines
2.0 KiB
Go

package config
import (
"log"
"os"
"github.com/joho/godotenv"
)
type Config struct {
Server ServerConfig
Database DatabaseConfig
Redis RedisConfig
JWT JWTConfig
SMTP SMTPConfig
}
type ServerConfig struct {
Port string
Mode string
}
type DatabaseConfig struct {
Driver string
Host string
Port string
User string
Password string
DBName string
SSLMode string
FilePath string
}
type RedisConfig struct {
Host string
Port string
Password string
DB int
}
type JWTConfig struct {
Secret string
ExpireHour int
}
type SMTPConfig struct {
Host string
Port string
User string
Password string
From string
}
var AppConfig *Config
func Load() {
_ = godotenv.Load()
AppConfig = &Config{
Server: ServerConfig{
Port: getEnv("SERVER_PORT", "8080"),
Mode: getEnv("SERVER_MODE", "debug"),
},
Database: DatabaseConfig{
Driver: getEnv("DB_DRIVER", "sqlite"),
Host: getEnv("DB_HOST", "localhost"),
Port: getEnv("DB_PORT", "5432"),
User: getEnv("DB_USER", "postgres"),
Password: getEnv("DB_PASSWORD", "postgres"),
DBName: getEnv("DB_NAME", "sale"),
SSLMode: getEnv("DB_SSLMODE", "disable"),
FilePath: getEnv("DB_FILEPATH", "data/sale.db"),
},
Redis: RedisConfig{
Host: getEnv("REDIS_HOST", "localhost"),
Port: getEnv("REDIS_PORT", "6379"),
Password: getEnv("REDIS_PASSWORD", ""),
DB: 0,
},
JWT: JWTConfig{
Secret: getEnv("JWT_SECRET", "sale-secret-key"),
ExpireHour: 72,
},
SMTP: SMTPConfig{
Host: getEnv("SMTP_HOST", ""),
Port: getEnv("SMTP_PORT", "587"),
User: getEnv("SMTP_USER", ""),
Password: getEnv("SMTP_PASSWORD", ""),
From: getEnv("SMTP_FROM", ""),
},
}
log.Printf("Config loaded: DB_DRIVER=%s, DB_FILEPATH=%s", AppConfig.Database.Driver, AppConfig.Database.FilePath)
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}