mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
Use the Express.js rate limiter middleware if not disabled
This commit is contained in:
57
src/index.ts
57
src/index.ts
@@ -6,6 +6,8 @@ import { InterchangeSystem, InterchangeSystemBuilderArguments } from "./entities
|
|||||||
import { ChapmanApiBasedParkingRepositoryLoader } from "./loaders/parking/ChapmanApiBasedParkingRepositoryLoader";
|
import { ChapmanApiBasedParkingRepositoryLoader } from "./loaders/parking/ChapmanApiBasedParkingRepositoryLoader";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import { expressMiddleware } from "@as-integrations/express5";
|
import { expressMiddleware } from "@as-integrations/express5";
|
||||||
|
import { RATE_LIMITS_DISABLED } from "./environment";
|
||||||
|
import rateLimit from "express-rate-limit";
|
||||||
|
|
||||||
const typeDefs = readFileSync("./schema.graphqls", "utf8");
|
const typeDefs = readFileSync("./schema.graphqls", "utf8");
|
||||||
|
|
||||||
@@ -36,24 +38,43 @@ async function main() {
|
|||||||
));
|
));
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(
|
const options = {
|
||||||
"/",
|
context: async () => {
|
||||||
express.json(),
|
return {
|
||||||
expressMiddleware(server, {
|
systems,
|
||||||
context: async () => {
|
findSystemById: (id: string) => {
|
||||||
return {
|
const system = systems.find((system) => system.id === id);
|
||||||
systems,
|
if (!system) {
|
||||||
findSystemById: (id: string) => {
|
return null;
|
||||||
const system = systems.find((system) => system.id === id);
|
}
|
||||||
if (!system) {
|
return system;
|
||||||
return null;
|
},
|
||||||
}
|
}
|
||||||
return system;
|
},
|
||||||
},
|
};
|
||||||
}
|
|
||||||
},
|
if (RATE_LIMITS_DISABLED) {
|
||||||
})
|
app.use(
|
||||||
);
|
"/",
|
||||||
|
express.json(),
|
||||||
|
expressMiddleware(server, options)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const limiter = rateLimit({
|
||||||
|
windowMs: 60 * 1000, // Every minute
|
||||||
|
limit: 20000,
|
||||||
|
standardHeaders: 'draft-8', // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header
|
||||||
|
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
|
||||||
|
ipv6Subnet: 60, // Set to 60 or 64 to be less aggressive, or 52 or 48 to be more aggressive
|
||||||
|
});
|
||||||
|
app.use(
|
||||||
|
"/",
|
||||||
|
express.json(),
|
||||||
|
limiter,
|
||||||
|
expressMiddleware(server, options),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const port = process.env.PORT ? parseInt(process.env.PORT) : 4000;
|
const port = process.env.PORT ? parseInt(process.env.PORT) : 4000;
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user