Skip to content

Commit 7982731

Browse files
committed
feat: rate limit
1 parent ea57670 commit 7982731

3 files changed

Lines changed: 17 additions & 21 deletions

File tree

backend/bun.lock

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

backend/index.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import express, { type NextFunction, type Request, type Response } from "express";
22
import cors from "cors"
33
import handleFile from "./routes/handleFile";
4-
// import expressStatusMonitor from 'express-status-monitor';
54
import morgan from "morgan";
5+
import { rateLimit } from "express-rate-limit";
66

77
const app = express();
88

@@ -26,6 +26,16 @@ app.use(cors({
2626
optionsSuccessStatus: 200,
2727
}));
2828

29+
const limiter = rateLimit({
30+
windowMs: 15 * 60 * 1000, // 15 minutes
31+
max: isProduction ? 100 : 1000,
32+
message: "Too many request from this IP, please try again later",
33+
standardHeaders: true,
34+
legacyHeaders: false
35+
})
36+
37+
app.use(limiter);
38+
2939
app.use(express.json({
3040
limit: '10mb', // Prevent large payload attacks
3141
verify: (req, res, buf) => {
@@ -42,26 +52,6 @@ app.use(express.urlencoded({ extended: true, limit: '10mb' }));
4252
app.use(morgan(isProduction ? 'combined' : 'dev', {
4353
skip: (req, res) => req.path === '/health' // Skip health check logs
4454
}));
45-
// if (!isProduction) {
46-
// app.use(expressStatusMonitor({
47-
// title: 'Candy Share API Status',
48-
// path: '/status',
49-
// spans: [
50-
// { interval: 1, retention: 60 }, // 1 minute intervals, keep 60 datapoints
51-
// { interval: 5, retention: 60 },
52-
// { interval: 15, retention: 60 }
53-
// ],
54-
// healthChecks: [
55-
// {
56-
// protocol: 'http',
57-
// host: 'localhost',
58-
// path: '/health',
59-
// port: PORT.toString()
60-
// }
61-
// ],
62-
// ignoreStartsWith: '/status' // Don't monitor the status page itself
63-
// }));
64-
// }
6555

6656
app.use((req: Request, res: Response, next: NextFunction) => {
6757
if (req.path !== '/health') {

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@types/morgan": "^1.9.10",
2929
"cors": "^2.8.5",
3030
"express": "^5.1.0",
31+
"express-rate-limit": "^8.0.1",
3132
"morgan": "^1.10.1"
3233
}
3334
}

0 commit comments

Comments
 (0)