from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from sqlalchemy import select from app.api.auth import router as auth_router from app.api.exports import router as exports_router from app.api.protocols import router as protocols_router from app.api.recordings import router as recordings_router from app.api.settings import router as settings_router from app.api.templates import router as templates_router from app.config import settings from app.database import async_session from app.messengers.telegram.bot import api_router as telegram_router, telegram_service from app.models.prompt_template import PromptTemplate from app.prompts.defaults import DEFAULT_TEMPLATES async def seed_default_templates(): async with async_session() as db: result = await db.execute(select(PromptTemplate)) if result.scalars().first() is not None: return for tpl in DEFAULT_TEMPLATES: db.add(PromptTemplate(**tpl)) await db.commit() @asynccontextmanager async def lifespan(app: FastAPI): await seed_default_templates() if settings.telegram_bot_token and settings.telegram_webhook_url.startswith("https"): await telegram_service.setup() yield if settings.telegram_bot_token and settings.telegram_webhook_url.startswith("https"): await telegram_service.shutdown() app = FastAPI(title="Meeting Protocol Service", lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(auth_router) app.include_router(recordings_router) app.include_router(protocols_router) app.include_router(templates_router) app.include_router(exports_router) app.include_router(settings_router) app.include_router(telegram_router) @app.get("/api/health") async def health(): return {"status": "ok"}