Skip to content

Commit debb5f2

Browse files
Add rate limiting
1 parent 74a1f55 commit debb5f2

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@types/node": "^24.10.13",
1111
"dotenv": "^17.3.1",
1212
"express": "^5.2.1",
13+
"express-rate-limit": "^8.3.1",
1314
"google-auth-library": "^10.5.0",
1415
"google-spreadsheet": "^5.2.0",
1516
"moment-timezone": "^0.6.0",

src/api/routes.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import express, { Express } from "express";
2+
import rateLimit from "express-rate-limit";
23
import slackbot from "../slackbot";
34
import { logWithTime } from "../utils/timeUtils";
45

56
export const registerApiRoutes = (app: Express) => {
67
app.use(express.json());
78

8-
app.post("/api/send-message", async (req, res) => {
9+
// Set up rate limiting
10+
const apiLimiter = rateLimit({
11+
windowMs: 15 * 60 * 1000, // 15 minutes
12+
max: 15, // Limit each IP to 15 requests per `window` (here, per 15 minutes)
13+
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
14+
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
15+
message: {
16+
success: false,
17+
error: "Too many requests, please try again later.",
18+
},
19+
});
20+
21+
app.post("/api/send-message", apiLimiter, async (req, res) => {
922
const authHeader = req.headers.authorization;
1023
const apiSecret = process.env.API_SECRET;
1124

0 commit comments

Comments
 (0)