This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
app.js
82 lines (73 loc) · 3.03 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const appInsights = require('applicationinsights');
const auth = require('./middleware/auth');
const bodyParser = require('body-parser');
const config = require('painless-config');
const express = require('express');
const logger = require('morgan');
const mockInsights = require('./providers/logger/mockInsights');
const CrawlerFactory = require('./lib/crawlerFactory');
const sendHelper = require('./middleware/sendHelper');
auth.initialize(config.get('CRAWLER_SERVICE_AUTH_TOKEN') || 'secret', config.get('CRAWLER_SERVICE_FORCE_AUTH'));
mockInsights.setup(config.get('CRAWLER_INSIGHTS_KEY') || 'mock', true);
const mode = config.get('CRAWLER_MODE') || '';
const service = CrawlerFactory.createService(mode);
const app = express();
app.disable('x-powered-by');
app.use(logger('dev'));
app.use(sendHelper());
// If we should be listening for webhooks, add the route before the json body parser so we get the raw bodies.
// Note also that the GitHub doc says events are capped at 25mb
app.use('/webhook', bodyParser.raw({ limit: '25mb', type: '*/*' }), require('./routes/webhook')(service, config.get('CRAWLER_WEBHOOK_SECRET')));
// It's safe to set limitation to 2mb.
app.use(bodyParser.json({ limit: '2mb' , strict: false}));
app.use('/status', require('./routes/status')(service));
app.use('/config', require('./routes/config')(service));
app.use('/requests', require('./routes/requests')(service));
app.use('/queues', require('./routes/queues')(service));
app.use('/deadletters', require('./routes/deadletters')(service));
// to keep AlwaysOn flooding logs with errors
app.get('/', function (request, response, next) {
response.helpers.send.noContent();
});
// Catch 404 and forward to error handler
const requestHandler = function (request, response, next) {
let error = { message: 'Not Found' };
error.status = 404;
error.success = false;
next(error);
};
app.use(requestHandler);
// Hang the service init code off a route middleware. Doesn't really matter which one.
requestHandler.init = (app, callback) => {
service.ensureInitialized().then(
() => {
service.run();
console.log('Service initialized');
// call the callback but with no args. An arg indicates an error.
callback();
},
error => {
console.log(`Service initialization error: ${error.message}`);
console.dir(error);
callback(error);
});
};
// Error handlers
const handler = function (error, request, response, next) {
appInsights.client.trackException(error, { name: 'SvcRequestFailure' });
if (response.headersSent) {
return next(error);
}
response.status(error.status || 500);
let propertiesToSerialize = ['success', 'message'];
if (app.get('env') !== 'production') {
propertiesToSerialize.push('stack');
}
// Properties on Error object aren't enumerable so need to explicitly list properties to serialize
response.send(JSON.stringify(error, propertiesToSerialize));
response.end();
};
app.use(handler);
module.exports = app;