69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
ServerPort string
|
|
Environment string
|
|
DBDriver string
|
|
DBPath string
|
|
JWTSecret []byte
|
|
JWTExpiryHours int64
|
|
EncryptionKey []byte
|
|
StoragePath string
|
|
MaxFileSizeMB int64
|
|
CORSAllowedOrigins []string
|
|
}
|
|
|
|
func Load() *Config {
|
|
// Загружаем .env файл (игнорируем ошибку если файла нет)
|
|
_ = godotenv.Load()
|
|
|
|
cfg := &Config{
|
|
ServerPort: getEnv("SERVER_PORT", "8080"),
|
|
Environment: getEnv("ENVIRONMENT", "development"),
|
|
DBDriver: getEnv("DB_DRIVER", "sqlite"),
|
|
DBPath: getEnv("DB_PATH", "./messenger.db"),
|
|
JWTSecret: []byte(getEnv("JWT_SECRET", "default-secret-key-change-me")),
|
|
JWTExpiryHours: getEnvAsInt64("JWT_EXPIRY_HOURS", 720),
|
|
EncryptionKey: []byte(getEnv("ENCRYPTION_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")),
|
|
StoragePath: getEnv("STORAGE_PATH", "./storage/attachments"),
|
|
MaxFileSizeMB: getEnvAsInt64("MAX_FILE_SIZE_MB", 20),
|
|
CORSAllowedOrigins: strings.Split(getEnv("CORS_ALLOWED_ORIGINS", "http://localhost:3000"), ","),
|
|
}
|
|
|
|
// Валидация ключа шифрования (должен быть 32 байта для AES-256)
|
|
if len(cfg.EncryptionKey) != 32 {
|
|
log.Printf("Warning: ENCRYPTION_KEY length is %d bytes, expected 32 bytes for AES-256", len(cfg.EncryptionKey))
|
|
}
|
|
|
|
// Создаём директорию для файлов если её нет
|
|
if err := os.MkdirAll(cfg.StoragePath, 0755); err != nil {
|
|
log.Printf("Warning: failed to create storage directory: %v", err)
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvAsInt64(key string, defaultValue int64) int64 {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intVal, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
return intVal
|
|
}
|
|
}
|
|
return defaultValue
|
|
} |