level 3 basics
This commit is contained in:
+16
-1
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user