-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (34 loc) · 1.27 KB
/
app.py
File metadata and controls
47 lines (34 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import hashlib
from contextlib import asynccontextmanager
from random import randbytes
from typing import Callable
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from routers import auth_router, exercises_router, health_router, tasks_router, texts_router
from service_logging import logger
@asynccontextmanager
async def lifespan(_: FastAPI):
# До запуска приложения
logger.info("FastAPI application starting up...")
yield
# После запуска
logger.info("FastAPI application shutting down...")
gateway = FastAPI(lifespan=lifespan)
@gateway.middleware("http")
async def add_request_hash(request: Request, call_next: Callable):
request_hash = hashlib.sha1(randbytes(32)).hexdigest()[:10]
with logger.contextualize(request_hash=request_hash):
response = await call_next(request)
return response
gateway.include_router(health_router)
gateway.include_router(auth_router)
gateway.include_router(texts_router)
gateway.include_router(tasks_router)
gateway.include_router(exercises_router)
gateway.add_middleware(
CORSMiddleware,
allow_origins=["*"], # TODO: Изменить в проде
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)