Files
tp/serv_nginx/api_bb/internal/config/config.go
T
2025-10-11 08:40:37 +05:00

50 lines
1.0 KiB
Go

// config/config.go
package config
import (
"fmt"
"os"
"github.com/joho/godotenv"
)
type Config struct {
Port string
DatabaseURL string
JWTSecret string
}
func Load() *Config {
_ = godotenv.Load(".env")
port := getEnv("PORT", "8080")
jwtSecret := getEnv("JWT_SECRET", "your-secret-key")
// Формируем DSN для PostgreSQL из переменных окружения
databaseURL := getPostgresDSN()
return &Config{
Port: port,
DatabaseURL: databaseURL,
JWTSecret: jwtSecret,
}
}
func getPostgresDSN() string {
host := getEnv("DB_HOST", "localhost")
port := getEnv("DB_PORT", "5432")
user := getEnv("DB_USER", "postgres")
password := getEnv("DB_PASSWORD", "postgres")
dbname := getEnv("DB_NAME", "bb_db")
sslmode := getEnv("DB_SSLMODE", "disable")
return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
host, port, user, password, dbname, sslmode)
}
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}