Compare commits

...

1 Commits

Author SHA1 Message Date
Timothy Farrell
0d3558da31 Add rate-limiting but it doesn't work. 2025-12-28 19:01:46 -06:00
3 changed files with 22 additions and 2 deletions

View File

@ -25,6 +25,11 @@ class Settings(BaseSettings):
dd_env: str = Field(default="development", alias="DD_ENV") dd_env: str = Field(default="development", alias="DD_ENV")
dd_version: str = Field(default="1.0.0", alias="DD_VERSION") dd_version: str = Field(default="1.0.0", alias="DD_VERSION")
# Rate limiting settings
rate_limit_enabled: bool = Field(default=True, alias="RATE_LIMIT_ENABLED")
rate_limit_requests: int = Field(default=100, alias="RATE_LIMIT_REQUESTS")
rate_limit_period: str = Field(default="1/minute", alias="RATE_LIMIT_PERIOD")
class Config: class Config:
env_file = ".env" env_file = ".env"
case_sensitive = False case_sensitive = False

View File

@ -12,7 +12,10 @@ from app.logging import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
# Rate limiter # Rate limiter
limiter = Limiter(key_func=get_remote_address) limiter = Limiter(
key_func=get_remote_address,
default_limits=[f"{settings.rate_limit_requests}/{settings.rate_limit_period}"] if settings.rate_limit_enabled else []
)
async def logging_middleware(request: Request, call_next: Callable) -> Response: async def logging_middleware(request: Request, call_next: Callable) -> Response:

14
main.py
View File

@ -5,10 +5,13 @@ import sentry_sdk
from ddtrace import patch_all from ddtrace import patch_all
from fastapi import APIRouter, FastAPI from fastapi import APIRouter, FastAPI
from sentry_sdk.integrations.fastapi import FastApiIntegration from sentry_sdk.integrations.fastapi import FastApiIntegration
from slowapi import _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from app.config import settings from app.config import settings
from app.logging import configure_logging, get_logger from app.logging import configure_logging, get_logger
from app.middleware import logging_middleware from app.middleware import logging_middleware, limiter
from app.resources import health from app.resources import health
@ -41,6 +44,15 @@ def create_app() -> FastAPI:
redoc_url="/redoc", redoc_url="/redoc",
) )
# Add rate limiting middleware if enabled
if settings.rate_limit_enabled:
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(SlowAPIMiddleware)
# Add logging middleware
app.middleware("http")(logging_middleware)
# Include all endpoint routers # Include all endpoint routers
app.include_router(health.router, tags=["health"]) app.include_router(health.router, tags=["health"])