Spaces:
Paused
Paused
superxuu commited on
Commit ·
b1dae5d
1
Parent(s): 5713453
feat: make api rate limits configurable via env vars
Browse files- backend/app/api.py +6 -4
backend/app/api.py
CHANGED
|
@@ -50,6 +50,8 @@ router = APIRouter(prefix="/api")
|
|
| 50 |
# 环境变量
|
| 51 |
ADMIN_SECRET = os.getenv("ADMIN_SECRET", "your-secret-key")
|
| 52 |
SESSION_EXPIRE_DAYS = int(os.getenv("SESSION_EXPIRE_DAYS", "30"))
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
class HealthResponse(BaseModel):
|
|
@@ -159,8 +161,8 @@ async def health_check():
|
|
| 159 |
|
| 160 |
|
| 161 |
@router.post("/v1/auth/register", response_model=AuthResponse)
|
| 162 |
-
@limiter.limit(
|
| 163 |
-
@limiter.limit(
|
| 164 |
async def register(request: Request, payload: RegisterRequest, db: Session = Depends(get_user_db)):
|
| 165 |
username = payload.username.strip()
|
| 166 |
if len(username) < 3 or len(payload.password) < 6:
|
|
@@ -185,8 +187,8 @@ async def register(request: Request, payload: RegisterRequest, db: Session = Dep
|
|
| 185 |
|
| 186 |
|
| 187 |
@router.post("/v1/auth/login", response_model=AuthResponse)
|
| 188 |
-
@limiter.limit(
|
| 189 |
-
@limiter.limit(
|
| 190 |
async def login(request: Request, payload: LoginRequest, db: Session = Depends(get_user_db)):
|
| 191 |
user = db.query(User).filter(User.username == payload.username.strip()).first()
|
| 192 |
if not user or not verify_password(payload.password, user.password_hash):
|
|
|
|
| 50 |
# 环境变量
|
| 51 |
ADMIN_SECRET = os.getenv("ADMIN_SECRET", "your-secret-key")
|
| 52 |
SESSION_EXPIRE_DAYS = int(os.getenv("SESSION_EXPIRE_DAYS", "30"))
|
| 53 |
+
RATE_LIMIT_GLOBAL = os.getenv("RATE_LIMIT_GLOBAL", "50/second")
|
| 54 |
+
RATE_LIMIT_IP = os.getenv("RATE_LIMIT_IP", "5/second")
|
| 55 |
|
| 56 |
|
| 57 |
class HealthResponse(BaseModel):
|
|
|
|
| 161 |
|
| 162 |
|
| 163 |
@router.post("/v1/auth/register", response_model=AuthResponse)
|
| 164 |
+
@limiter.limit(RATE_LIMIT_GLOBAL, key_func=lambda: "global")
|
| 165 |
+
@limiter.limit(RATE_LIMIT_IP)
|
| 166 |
async def register(request: Request, payload: RegisterRequest, db: Session = Depends(get_user_db)):
|
| 167 |
username = payload.username.strip()
|
| 168 |
if len(username) < 3 or len(payload.password) < 6:
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
@router.post("/v1/auth/login", response_model=AuthResponse)
|
| 190 |
+
@limiter.limit(RATE_LIMIT_GLOBAL, key_func=lambda: "global")
|
| 191 |
+
@limiter.limit(RATE_LIMIT_IP)
|
| 192 |
async def login(request: Request, payload: LoginRequest, db: Session = Depends(get_user_db)):
|
| 193 |
user = db.query(User).filter(User.username == payload.username.strip()).first()
|
| 194 |
if not user or not verify_password(payload.password, user.password_hash):
|