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)
|
||||
|
||||
10
main.py
10
main.py
@@ -37,12 +37,11 @@ from app.service.osu_rx_statistics import create_rx_statistics
|
||||
from app.service.redis_message_system import redis_message_system
|
||||
from app.utils import bg_tasks, utcnow
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi_limiter import FastAPILimiter
|
||||
from fastapi_limiter.depends import RateLimiter
|
||||
import sentry_sdk
|
||||
|
||||
|
||||
@@ -152,13 +151,6 @@ app = FastAPI(
|
||||
lifespan=lifespan,
|
||||
description=desc,
|
||||
)
|
||||
if settings.enable_rate_limit:
|
||||
app.router.dependencies.extend(
|
||||
[
|
||||
Depends(RateLimiter(times=1200, minutes=1)),
|
||||
Depends(RateLimiter(times=200, seconds=1)),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
app.include_router(api_v2_router)
|
||||
|
||||
Reference in New Issue
Block a user