85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from functools import lru_cache
|
|
|
|
|
|
def _to_bool(value: str | None, default: bool) -> bool:
|
|
if value is None:
|
|
return default
|
|
return value.strip().lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
def _to_list(value: str | None) -> list[str]:
|
|
if not value:
|
|
return []
|
|
return [item.strip() for item in value.split(",") if item.strip()]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Settings:
|
|
app_name: str = "MAX Support Bot"
|
|
app_env: str = "dev"
|
|
app_host: str = "0.0.0.0"
|
|
app_port: int = 8000
|
|
|
|
db_path: str = "data/app.db"
|
|
session_secret: str = "change-me-session-secret"
|
|
|
|
web_admin_login: str = "admin"
|
|
web_admin_password: str = "admin"
|
|
|
|
max_bot_token: str = ""
|
|
max_bot_api_url: str = "https://botapi.max.ru"
|
|
max_use_webhook: bool = False
|
|
max_webhook_url: str = ""
|
|
max_webhook_secret: str = ""
|
|
|
|
context_backend: str = "memory"
|
|
redis_url: str = "redis://localhost:6379/0"
|
|
|
|
channel_local_enabled: bool = True
|
|
channel_external_enabled: bool = True
|
|
external_api_create_url_with_key: str = ""
|
|
|
|
operator_chat_ids: list[int] | None = None
|
|
request_categories: list[str] | None = None
|
|
|
|
@classmethod
|
|
def from_env(cls) -> "Settings":
|
|
operator_ids = [int(v) for v in _to_list(os.getenv("OPERATOR_CHAT_IDS")) if v.isdigit()]
|
|
categories = _to_list(os.getenv("REQUEST_CATEGORIES")) or [
|
|
"Общая консультация",
|
|
"Техническая проблема",
|
|
"Оплата",
|
|
"Другое",
|
|
]
|
|
return cls(
|
|
app_name=os.getenv("APP_NAME", "MAX Support Bot"),
|
|
app_env=os.getenv("APP_ENV", "dev"),
|
|
app_host=os.getenv("APP_HOST", "0.0.0.0"),
|
|
app_port=int(os.getenv("APP_PORT", "8000")),
|
|
db_path=os.getenv("DB_PATH", "data/app.db"),
|
|
session_secret=os.getenv("SESSION_SECRET", "change-me-session-secret"),
|
|
web_admin_login=os.getenv("WEB_ADMIN_LOGIN", "admin"),
|
|
web_admin_password=os.getenv("WEB_ADMIN_PASSWORD", "admin"),
|
|
max_bot_token=os.getenv("MAX_BOT_TOKEN", "").strip(),
|
|
max_bot_api_url=os.getenv("MAX_BOT_API_URL", "https://botapi.max.ru"),
|
|
max_use_webhook=_to_bool(os.getenv("MAX_USE_WEBHOOK"), False),
|
|
max_webhook_url=os.getenv("MAX_WEBHOOK_URL", "").strip(),
|
|
max_webhook_secret=os.getenv("MAX_WEBHOOK_SECRET", "").strip(),
|
|
context_backend=os.getenv("CONTEXT_BACKEND", "memory").strip().lower(),
|
|
redis_url=os.getenv("REDIS_URL", "redis://localhost:6379/0"),
|
|
channel_local_enabled=_to_bool(os.getenv("CHANNEL_LOCAL_ENABLED"), True),
|
|
channel_external_enabled=_to_bool(os.getenv("CHANNEL_EXTERNAL_ENABLED"), True),
|
|
external_api_create_url_with_key=os.getenv("EXTERNAL_API_CREATE_URL_WITH_KEY", "").strip(),
|
|
operator_chat_ids=operator_ids,
|
|
request_categories=categories,
|
|
)
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def get_settings() -> Settings:
|
|
return Settings.from_env()
|
|
|