-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (52 loc) · 1.67 KB
/
app.js
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
59
60
61
// app.js
// Main entry point for the GitHub webhook dispatcher
// Create an express API server to consume GitHub webhook payload events and route them to the appropriate downstream service
const express = require('express');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const app = express();
const config = require('./config');
const lib = require('./lib');
const RateLimit = require('express-rate-limit');
const console = require('console');
// Configure rate limiter
const limiter = RateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 60, // limit each IP to 60 requests per windowMs
});
// App Express configuration
// Parse the request body as JSON
app.use(bodyParser.json());
// Set options for Express
app.disable('x-powered-by');
app.use(helmet());
// Create a route for the GitHub webhook
app.post('/', (req, res) => {
lib.webhookHandler(req, res);
});
// Create a route to get a list of all the configured routes
app.get('/routes', (req, res) => {
lib.listRouteHandler(req, res);
});
// Create a route to serve the openapi spec
// Apply rate limiting to this route
app.use('/openapi.json', limiter);
app.get('/openapi.json', (req, res) => {
lib.openapiHandler(req, res);
});
// Create a dummy route for health checks
app.post('/health', (req, res) => {
res.statusCode = 200;
res.send('alive');
});
app.post('/dummy1', (req, res) => {
res.statusCode = 200;
res.send('dummy1');
});
app.post('/dummy2', (req, res) => {
res.statusCode = 200;
res.send('dummy2');
});
// Start the server
app.listen(config.port, () => console.log(`GitHub webhook dispatcher listening on port ${config.port}!`));
console.log(`Debug mode: ${config.debug}`);