-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (37 loc) · 1.17 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
from contextlib import asynccontextmanager
from fastapi import FastAPI
from ratelimit import RateLimitMiddleware, Rule
from rate_limit_client import client_ip
from ratelimit.backends.simple import MemoryBackend
from db.config import engine, Base
from routers import message_router, user_router
MEMORY_BACKEND = MemoryBackend()
@asynccontextmanager
async def lifespan(app: FastAPI):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
app = FastAPI(lifespan=lifespan)
app.add_middleware(
RateLimitMiddleware,
authenticate=client_ip,
backend=MEMORY_BACKEND,
config={
r"^/messages": [Rule(minute=10)],
},
)
app.include_router(user_router.router)
app.include_router(message_router.router)
""""
It's deprecated
"""
# @app.on_event("startup")
# async def startup():
# async with engine.begin() as conn:
# await conn.run_sync(Base.metadata.create_all)
# @app.on_event('shutdown')
# async def shutdown_event():
# async with engine.begin() as conn:
# await conn.run_sync(Base.metadata.drop_all)