Skip to content

Commit

Permalink
Refactor package dependencies and add rate limiting to server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kroonen committed Sep 8, 2024
1 parent 9952303 commit f2bd961
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dompurify": "^3.1.6",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-rate-limit": "^7.4.0",
"lucide-react": "^0.439.0",
"mermaid": "^11.1.1",
"openai": "^4.58.1",
Expand Down
14 changes: 14 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { exec } = require("child_process");
const path = require("path");
const swaggerUi = require("swagger-ui-express");
const swaggerJsdoc = require("swagger-jsdoc");
const rateLimit = require('express-rate-limit');

const USER_PREFERENCES_FILE = path.join(__dirname, "user_preferences.json");

Expand Down Expand Up @@ -126,6 +127,19 @@ async function initializeApiClients() {
}

const app = express();
app.set('trust proxy', 1); // Adjust this based on your environment

const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
skipFailedRequests: true, // Skip limiting if request fails
keyGenerator: (req, res) => req.ip, // Use the request IP as key
});

app.use(apiLimiter);

app.use(express.json());

const artifactManager = new ArtifactManager();
Expand Down

0 comments on commit f2bd961

Please sign in to comment.