48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from pathlib import Path
|
|
|
|
from pydantic import field_validator
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(
|
|
env_file=BASE_DIR / ".env",
|
|
env_file_encoding="utf-8",
|
|
extra="ignore",
|
|
)
|
|
|
|
bot_token: str
|
|
# Same API the web cabinet uses (bot no longer opens SQLite).
|
|
api_base_url: str = "http://127.0.0.1:51291"
|
|
api_token: str
|
|
# Example for Docker → host Xray: socks5://127.0.0.1:10808
|
|
proxy_url: str | None = None
|
|
|
|
@field_validator("proxy_url", mode="before")
|
|
@classmethod
|
|
def empty_proxy_as_none(cls, value: object) -> object:
|
|
if value is None:
|
|
return None
|
|
if isinstance(value, str) and not value.strip():
|
|
return None
|
|
return value
|
|
|
|
@field_validator("api_token", mode="before")
|
|
@classmethod
|
|
def require_api_token(cls, value: object) -> object:
|
|
if value is None or (isinstance(value, str) and not value.strip()):
|
|
raise ValueError(
|
|
"API_TOKEN is required: bot authenticates to the API with it"
|
|
)
|
|
return value
|
|
|
|
@field_validator("api_base_url")
|
|
@classmethod
|
|
def normalize_base_url(cls, value: str) -> str:
|
|
return value.rstrip("/")
|
|
|
|
|
|
settings = Settings()
|