-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
39 lines (32 loc) · 1.17 KB
/
Copy pathmain.py
File metadata and controls
39 lines (32 loc) · 1.17 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
from app import limiter
from app.routers import *
from config import settings
from app.utils import rabbitmq_service
from app.database.base import async_main
from app.database.redis import redis_service
import aiohttp
from fastapi import FastAPI
from contextlib import asynccontextmanager
from slowapi.errors import RateLimitExceeded
from slowapi import _rate_limit_exceeded_handler
from fastapi.middleware.cors import CORSMiddleware
@asynccontextmanager
async def lifespan(app: FastAPI):
# Run at startup
await async_main()
app.state.session = aiohttp.ClientSession()
await redis_service.connect()
# await rabbitmq_service.connect()
yield
# Run on shutdown
await app.state.session.close()
await redis_service.close()
# await rabbitmq_service.close()
app = FastAPI(title=settings.TITLE, version=settings.VERSION, root_path=settings.ROOT_PATH, lifespan=lifespan)
app.add_middleware(CORSMiddleware,
allow_origins=["*"],
allow_methods=['*'],
allow_headers=['*'])
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.state.limiter = limiter
app.include_router(example_router)