104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
Redis RedisConfig
|
|
JWT JWTConfig
|
|
DocumentService DocumentServiceConfig
|
|
Log LogConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string
|
|
Host string
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Host string
|
|
Port string
|
|
User string
|
|
Password string
|
|
DBName string
|
|
SSLMode string
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Host string
|
|
Port string
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
type JWTConfig struct {
|
|
Secret string
|
|
Expiration int // в часах
|
|
}
|
|
|
|
type DocumentServiceConfig struct {
|
|
URL string
|
|
}
|
|
|
|
type LogConfig struct {
|
|
Level string
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
// Загрузка .env файла если существует
|
|
godotenv.Load()
|
|
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: getEnv("SERVER_PORT", "8080"),
|
|
Host: getEnv("SERVER_HOST", "0.0.0.0"),
|
|
},
|
|
Database: DatabaseConfig{
|
|
Host: getEnv("DB_HOST", "localhost"),
|
|
Port: getEnv("DB_PORT", "5432"),
|
|
User: getEnv("DB_USER", "erp_user"),
|
|
Password: getEnv("DB_PASSWORD", "erp_pass"),
|
|
DBName: getEnv("DB_NAME", "erp_mvp"),
|
|
SSLMode: getEnv("DB_SSLMODE", "disable"),
|
|
},
|
|
Redis: RedisConfig{
|
|
Host: getEnv("REDIS_HOST", "localhost"),
|
|
Port: getEnv("REDIS_PORT", "6379"),
|
|
Password: getEnv("REDIS_PASSWORD", ""),
|
|
DB: getEnvAsInt("REDIS_DB", 0),
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: getEnv("JWT_SECRET", "your-secret-key"),
|
|
Expiration: getEnvAsInt("JWT_EXPIRATION", 24),
|
|
},
|
|
DocumentService: DocumentServiceConfig{
|
|
URL: getEnv("DOC_SERVICE_URL", "http://localhost:8000"),
|
|
},
|
|
Log: LogConfig{
|
|
Level: getEnv("LOG_LEVEL", "info"),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsInt(key string, defaultValue int) int {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intValue, err := strconv.Atoi(value); err == nil {
|
|
return intValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|