level 3 basics

This commit is contained in:
2026-10-26 16:42:32 +03:00
parent 3bd25a9127
commit 9d3de3b2d9
16 changed files with 703 additions and 126 deletions
+16 -1
View File
@@ -2,6 +2,7 @@ import json
import logging
import re
import uuid
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI, Request
@@ -10,7 +11,9 @@ from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from .config import load_settings
from .db import open_pool
from .errors import ApiError
from .migrate import apply_migrations
from .routes import router
from .store import Store
@@ -51,10 +54,23 @@ def _request_id(request: Request) -> str:
_OPENAPI_PATH = Path(__file__).resolve().parents[1] / 'contracts' / 'openapi.json'
@asynccontextmanager
async def lifespan(app: FastAPI):
settings = load_settings()
pool = await open_pool(settings.database_url)
try:
await apply_migrations(pool)
app.state.store = Store(pool, settings.session_ttl_seconds)
yield
finally:
await pool.close()
def create_app() -> FastAPI:
app = FastAPI(
title='MTUSI Messenger lab2',
version='1.2.0',
lifespan=lifespan,
)
def openapi():
@@ -63,7 +79,6 @@ def create_app() -> FastAPI:
return app.openapi_schema
app.openapi = openapi
app.state.store = Store(load_settings().session_ttl_seconds)
@app.middleware('http')
async def request_context(request: Request, call_next):