harshraj22 / Dis-Sim

Microservice for calculating image similarity. Uses Message Queues.
MIT License
9 stars 3 forks source link

Add Rate Limiter #3

Open harshraj22 opened 2 years ago

harshraj22 commented 2 years ago

Add rate limiter to the api endpoint.

# https://slowapi.readthedocs.io/en/latest/
from fastapi import FastAPI, Request
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.middleware import SlowAPIMiddleware
from slowapi.errors import RateLimitExceeded

app = FastAPI()
# limiter = Limiter(key_func=get_remote_address, default_limits=["1/minute"])
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

@app.get("/")
@limiter.limit("1/minute")
def read_root(request: Request):
    return {"Hello": "World"}
harshraj22 commented 1 year ago

Add the rate limiter to the auth service. the other endpoints are/will be protected by API Gateway/ subscription based rate limiter.