Files
tgit/app/config.py
T
ElebobaandClaude Opus 5.5 f022731a5d feat(bot): модель данных, миграции Alembic, FSM в БД, TTL-чистка
- app/db/models.py: 13 таблиц (ARCHITECTURE §2.10); перечисления — VARCHAR + CHECK,
  JSON → JSONB на Postgres, BIGINT/INTEGER-ключи для SQLite
- миграция 0001 (автогенерация + ручная чистка: импорт sa.Text, дубли CHECK)
- миграции применяются при старте бота программно (app.db.migrate), script_location из пакета
- SqlStorage: FSM aiogram поверх fsm_state — состояние переживает рестарт (перенесено из #4)
- cleanup_loop: истёкшие черновики, снимки контейнеров и попытки входа старше суток
- тесты: upgrade/downgrade, дрейф моделей и миграций, CHECK, CASCADE, FSM, чистка

Refs #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:47:22 +03:00

58 lines
2.0 KiB
Python

"""Typed configuration from the environment / `.env` — one settings class per subsystem (ARCHITECTURE §2.11)."""
from pathlib import Path
from typing import Annotated
from pydantic import Field, SecretStr, field_validator
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
_ENV = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
class TelegramSettings(BaseSettings):
model_config = SettingsConfigDict(**_ENV, env_prefix="TELEGRAM_")
bot_token: SecretStr
class AccessSettings(BaseSettings):
model_config = _ENV
# Bootstrap admins (SPEC A-8). Comma-separated in the env: ADMIN_IDS=1,2
admin_ids: Annotated[frozenset[int], NoDecode] = frozenset()
@field_validator("admin_ids", mode="before")
@classmethod
def _split_ids(cls, value: object) -> object:
if isinstance(value, str):
return frozenset(int(part) for part in value.replace(" ", "").split(",") if part)
return value
class DatabaseSettings(BaseSettings):
model_config = SettingsConfigDict(**_ENV, env_prefix="DATABASE_")
# Compose sets a PostgreSQL URL; the default lets the bot run locally without Docker.
url: str = "sqlite+aiosqlite:///./tgit.db"
class RuntimeSettings(BaseSettings):
model_config = _ENV
log_level: str = "INFO"
# Non-secret configs (texts, thresholds, prompts). Relative to the working dir: /srv/tgit in Docker.
config_dir: Path = Path("config")
# The heartbeat file is touched while the event loop is alive; the Docker healthcheck reads its age.
heartbeat_file: Path = Path("/tmp/tgit-heartbeat")
heartbeat_interval: float = Field(default=10.0, gt=0)
heartbeat_max_age: float = Field(default=30.0, gt=0)
class Settings(BaseSettings):
model_config = _ENV
telegram: TelegramSettings = Field(default_factory=TelegramSettings)
access: AccessSettings = Field(default_factory=AccessSettings)
database: DatabaseSettings = Field(default_factory=DatabaseSettings)
runtime: RuntimeSettings = Field(default_factory=RuntimeSettings)