120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
RunMode RunMode
|
|
DebugMode bool
|
|
BotToken string
|
|
|
|
DBConnectionString string
|
|
|
|
OCRTokenPath string
|
|
|
|
TelegramApi string
|
|
|
|
APIPort string
|
|
APIHost string
|
|
APISecret string
|
|
OpenAPIEnabled bool
|
|
OpenAPIEndpoint string
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
_ = godotenv.Load()
|
|
var warnings []string
|
|
|
|
mode := os.Getenv("RUN_MODE")
|
|
debugMode := os.Getenv("DEBUG_MODE") == "true"
|
|
botToken := os.Getenv("BOT_TOKEN")
|
|
ocrTokenPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
|
|
apiPort := os.Getenv("API_PORT")
|
|
apiHost := os.Getenv("API_HOST")
|
|
apiSecret := os.Getenv("API_SECRET")
|
|
openAPIEnabled := os.Getenv("OPEN_API_ENABLED") == "true"
|
|
openAPIEndpoint := os.Getenv("OPEN_API_ENDPOINT")
|
|
|
|
runMode, err := ParseRunMode(mode)
|
|
dbConnectionString := buildConnectionString()
|
|
if err != nil {
|
|
warnings = append(warnings, err.Error())
|
|
}
|
|
|
|
if runMode == Bot || runMode == Standalone {
|
|
if ocrTokenPath == "" {
|
|
warnings = append(warnings, "Missing required environment variable: GOOGLE_APPLICATION_CREDENTIALS")
|
|
}
|
|
if botToken == "" {
|
|
warnings = append(warnings, "Missing required environment variable: BOT_TOKEN")
|
|
}
|
|
}
|
|
if runMode == API || runMode == Standalone {
|
|
if ocrTokenPath == "" {
|
|
warnings = append(warnings, "Missing required environment variable: GOOGLE_APPLICATION_CREDENTIALS")
|
|
}
|
|
if apiSecret == "" {
|
|
warnings = append(warnings, "Missing required environment variable: API_SECRET")
|
|
}
|
|
if apiHost == "" {
|
|
apiHost = "localhost"
|
|
}
|
|
if apiPort == "" {
|
|
apiPort = "8000"
|
|
}
|
|
if openAPIEndpoint == "" {
|
|
openAPIEndpoint = "/openapi"
|
|
}
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
|
return Config{}, errors.New(strings.Join(warnings, "\n"))
|
|
}
|
|
return Config{
|
|
BotToken: botToken,
|
|
DBConnectionString: dbConnectionString,
|
|
OCRTokenPath: ocrTokenPath,
|
|
DebugMode: debugMode,
|
|
RunMode: runMode,
|
|
APIPort: apiPort,
|
|
APIHost: apiHost,
|
|
APISecret: apiSecret,
|
|
OpenAPIEnabled: openAPIEnabled,
|
|
OpenAPIEndpoint: openAPIEndpoint,
|
|
TelegramApi: "https://api.telegram.org",
|
|
}, nil
|
|
}
|
|
|
|
func buildConnectionString() string {
|
|
// если задана готовая строка — используем её (удобно для локальной разработки через .env)
|
|
if dsn := os.Getenv("DB_PATH"); dsn != "" {
|
|
return dsn
|
|
}
|
|
|
|
// собираем из отдельных переменных (для Kubernetes)
|
|
host := os.Getenv("DB_HOST")
|
|
port := os.Getenv("DB_PORT")
|
|
user := os.Getenv("DB_USER")
|
|
password := os.Getenv("DB_PASSWORD")
|
|
dbName := os.Getenv("DB_NAME")
|
|
|
|
if host == "" || user == "" || password == "" || dbName == "" {
|
|
return ""
|
|
}
|
|
|
|
if port == "" {
|
|
port = "5432"
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
|
user, password, host, port, dbName,
|
|
)
|
|
}
|