-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (47 loc) · 1.51 KB
/
main.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from database import create_tables, delete_tables
from router import router as links_router
from auth import auth_router
from contextlib import asynccontextmanager
from repository import delete_expired_links
import asyncio
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
await delete_tables()
logger.info("База очищена")
await create_tables()
logger.info("База готова к работе")
asyncio.create_task(delete_expired_links())
yield
logger.info("Выключение")
app = FastAPI(lifespan=lifespan)
app.include_router(links_router)
app.include_router(auth_router)
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="URL Shortener API",
version="1.0.0",
description="API для сокращения ссылок",
routes=app.routes,
)
openapi_schema["components"]["securitySchemes"] = {
"OAuth2PasswordBearer": {
"type": "oauth2",
"flows": {
"password": {
"tokenUrl": "/auth/token",
"scopes": {}
}
}
}
}
openapi_schema["security"] = [{"OAuth2PasswordBearer": []}]
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi