Files
iptvc/app/config/config.go
AnthonyAxenov 303ccdd02b
Some checks failed
release / release (push) Failing after 51m35s
Версия 1.0.0
2025-05-09 17:58:40 +08:00

78 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2025, Антон Аксенов
* This file is part of iptvc project
* MIT License: https://git.axenov.dev/IPTV/iptvc/src/branch/master/LICENSE
*/
package config
import (
"github.com/joho/godotenv"
"os"
"strconv"
)
// Config описывает конфигурацию
type Config struct {
DebugMode bool
Cache CacheConfig
}
// CacheConfig описывает конфигурацию подключения к keydb
type CacheConfig struct {
IsEnabled bool
Host string
Port uint
Username string
Password string
Db uint
Ttl uint
IsActive bool
}
// Init инициализирует объект конфигурации из переменных окружения
func Init() *Config {
_ = godotenv.Load(".env")
return &Config{
//DebugMode: readEnvBoolean("APP_DEBUG", false),
Cache: CacheConfig{
IsEnabled: readEnvBoolean("CACHE_ENABLED", false),
Host: readEnv("CACHE_HOST", "localhost"),
Port: readEnvInteger("CACHE_PORT", 6379),
Username: readEnv("CACHE_USERNAME", ""),
Password: readEnv("CACHE_PASSWORD", ""),
Db: readEnvInteger("CACHE_DB", 0),
Ttl: readEnvInteger("CACHE_TTL", 1800),
},
}
}
// readEnv считывает строковую переменную окружения с заданным именем или возвращает значение по умолчанию
func readEnv(key string, defaultValue string) string {
value, exists := os.LookupEnv(key)
if exists {
return value
}
return defaultValue
}
// readEnvBoolean считывает булеву переменную окружения с заданным именем или возвращает значение по умолчанию
func readEnvBoolean(name string, defaultValue bool) bool {
valStr := readEnv(name, "")
val, err := strconv.ParseBool(valStr)
if err == nil {
return val
}
return defaultValue
}
// readEnvInteger считывает целочисленную переменную окружения с заданным именем или возвращает значение по умолчанию
func readEnvInteger(name string, defaultValue uint) uint {
valueStr := readEnv(name, "")
value, err := strconv.Atoi(valueStr)
if err == nil {
return uint(value)
}
return defaultValue
}