fix(rate-limit): apply on router to avoid TypeError on ws endpoint
This commit is contained in:
14
app/dependencies/rate_limit.py
Normal file
14
app/dependencies/rate_limit.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.config import settings
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi_limiter.depends import RateLimiter
|
||||
|
||||
if settings.enable_rate_limit:
|
||||
LIMITERS = [
|
||||
Depends(RateLimiter(times=1200, minutes=1)),
|
||||
Depends(RateLimiter(times=200, seconds=1)),
|
||||
]
|
||||
else:
|
||||
LIMITERS = []
|
||||
@@ -1,5 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.rate_limit import LIMITERS
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/api/private")
|
||||
router = APIRouter(prefix="/api/private", dependencies=LIMITERS)
|
||||
|
||||
@@ -3,12 +3,13 @@ from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from app.dependencies.rate_limit import LIMITERS
|
||||
from app.dependencies.user import v1_authorize
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel, field_serializer
|
||||
|
||||
router = APIRouter(prefix="/api/v1", dependencies=[Depends(v1_authorize)], tags=["V1 API"])
|
||||
router = APIRouter(prefix="/api/v1", dependencies=[Depends(v1_authorize), *LIMITERS], tags=["V1 API"])
|
||||
|
||||
|
||||
class AllStrModel(BaseModel):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.rate_limit import LIMITERS
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/api/v2")
|
||||
|
||||
# 导入所有子路由模块来注册路由
|
||||
router = APIRouter(prefix="/api/v2", dependencies=LIMITERS)
|
||||
|
||||
Reference in New Issue
Block a user