Skip to content

Commit

Permalink
Make default limit configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexguo8 committed Feb 16, 2022
1 parent 17fb5a6 commit c615443
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 8 additions & 1 deletion backend/python/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ def create_app(config_name):
app.config["CORS_SUPPORTS_CREDENTIALS"] = True
CORS(app)

Limiter(app, key_func=get_remote_address, default_limits=["15 per minute"])
default_minute_rate_limit = (
os.getenv("BACKEND_API_DEFAULT_PER_MINUTE_RATE_LIMIT") or 15
)
Limiter(
app,
key_func=get_remote_address,
default_limits=[f"{default_minute_rate_limit} per minute"],
)

if os.getenv("FLASK_CONFIG") != "production":
app.config[
Expand Down
7 changes: 6 additions & 1 deletion backend/typescript/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ const rateLimitRule = createRateLimitRule({
formatError: () => "Too many requests, please try again later.",
});

const defaultMinuteRateLimit = parseInt(
process.env.BACKEND_API_DEFAULT_PER_MINUTE_RATE_LIMIT || "15",
10,
);

const rateLimiters = shield(
{},
{
fallbackRule: rateLimitRule({
window: "1m",
max: 15,
max: defaultMinuteRateLimit,
}),
},
);
Expand Down
6 changes: 5 additions & 1 deletion backend/typescript/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ const CORS_OPTIONS: cors.CorsOptions = {

const swaggerDocument = YAML.load("swagger.yml");

const defaultMinuteRateLimit = parseInt(
process.env.BACKEND_API_DEFAULT_PER_MINUTE_RATE_LIMIT || "15",
10,
);
const limiter = RateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 15,
max: defaultMinuteRateLimit,
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
Expand Down

0 comments on commit c615443

Please sign in to comment.