-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (46 loc) · 1.63 KB
/
app.js
File metadata and controls
59 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// import dependencies
import express from "express";
import YAML from "yamljs";
import swaggerUI from "swagger-ui-express";
import cors from "cors";
import helmet from "helmet";
import {rateLimit} from "express-rate-limit";
import v1Router from "./src/routes/v1/index.js";
import v2Router from "./src/routes/v2/index.js";
// import routes (API contructors)
const BASE_URI = "https://data.designmuseumgent.be/v1/";
// setup accept-headers
const app = express();
// behind proxies (e.g., Heroku) so rate limiting and IP work correctly
app.set('trust proxy', 1);
// security headers
app.disable('x-powered-by');
app.use(helmet());
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // one minute
limit: 10, // limit public requests to 10 per minute
message: {
status: 429,
message: "Too many requests, please slow down."
},
legacyHeaders: false,
standardHeaders: 'draft-8',
});
app.use(limiter);
// CORS: allowlist from env (comma-separated). Default to disabled if not provided.
const allowedOrigins = (process.env.CORS_ORIGINS || '').split(',').map(s => s.trim()).filter(Boolean);
const corsOptions = {
origin: allowedOrigins.length ? allowedOrigins : true, // default: allow all (backward compatible); restrict via CORS_ORIGINS
methods: ["GET", "HEAD", "OPTIONS"],
credentials: false,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.use(express.static("public"));
// swagger docs
const swaggerDocument = YAML.load("./api.yaml");
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerDocument));
// routes
app.use("/v1", v1Router);
app.use("/v2", v2Router);
export default app;