Files
md2vk/app/main.py
T

48 lines
1.4 KiB
Python

"""md2vk — FastAPI приложение."""
from __future__ import annotations
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from app.database import init_db
from app.api.v1 import router as api_v1_router
from app.api.audit import AuditMiddleware
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
logger = logging.getLogger("md2vk")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Инициализация при старте."""
logger.info("Initializing database...")
await init_db()
logger.info("Database ready. Starting md2vk...")
yield
logger.info("Shutting down...")
app = FastAPI(
title="md2vk",
description="Сервис публикации Markdown на стене VK",
version="0.1.0",
lifespan=lifespan,
)
# Аудит-лог авторизации/запросов API (JSONL, ротация по дням)
app.add_middleware(AuditMiddleware)
app.include_router(api_v1_router)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled error: {exc}", exc_info=True)
return JSONResponse(
status_code=500,
content={"detail": f"Internal server error: {str(exc)}"},
)