-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
176 lines (143 loc) · 4.29 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @import {Application} from 'express'
* @import {AppContainer, AppOptions, Callback} from './typings/n-express'
*/
require('isomorphic-fetch');
const fs = require('fs');
const path = require('path');
const express = require('express');
// flags
const flags = require('@financial-times/n-flags-client');
// backend authentication
const backendAuthentication = require('./src/middleware/backend-authentication');
// consent
const consentMiddleware = require('./src/middleware/consent');
// logging and monitoring
const metrics = require('next-metrics');
const logger = require('@dotcom-reliability-kit/logger');
// utils
const setupHealthEndpoint = require('./src/lib/health-checks');
const InstrumentListen = require('./src/lib/instrument-listen');
const guessAppDetails = require('./src/lib/guess-app-details');
const { cache } = require('./src/middleware/cache');
const robots = require('./src/middleware/robots');
const security = require('./src/middleware/security');
const vary = require('./src/middleware/vary');
const logVary = require('./src/middleware/log-vary');
const anon = require('./src/middleware/anon');
const teapot = fs.readFileSync(
path.join(__dirname, 'src/assets/teapot.ascii'),
'utf8'
);
const generateViaMiddleware = require('./src/middleware/via');
/**
* @param {AppOptions} options
* @returns {AppContainer}
*/
const getAppContainer = (options) => {
options = Object.assign(
{},
{
withBackendAuthentication: true,
withFlags: false,
withConsent: false,
withServiceMetrics: true,
healthChecks: []
},
options || {}
);
if (!options.systemCode) {
throw new Error(
'All applications must specify a Biz Ops `systemCode` to the express() function. See the README for more details.'
);
}
if (options.withAb) {
logger.warn({
event: 'WITHAB_OPTION_DEPRECATED',
message: 'The \'withAb\' option is deprecated and no longer supported by n-express or n-flags-client'
});
}
const meta = guessAppDetails(options);
/** @type {Promise<any>[]} */
const initPromises = [];
const instrumentListen = new InstrumentListen(express(), meta, initPromises);
const app = instrumentListen.app;
const addInitPromise = initPromises.push.bind(initPromises);
app.get('/robots.txt', robots);
app.get(
'/__brew-coffee',
/** @type {Callback} */ (req, res) => {
res.status(418);
res.send(teapot);
res.end();
}
);
// Security related headers, see https://securityheaders.io/?q=https%3A%2F%2Fwww.ft.com&hide=on.
app.set('x-powered-by', false);
app.use(security);
// Add the application system code to the Via HTTP header
app.use(generateViaMiddleware(meta.systemCode));
// utility middleware
app.use(vary);
if (!options.demo) {
setupHealthEndpoint(app, options, meta);
}
// Debug related headers.
app.use(
/** @type {Callback} */ (req, res, next) => {
res.set('FT-App-Name', meta.name);
res.set('FT-Backend-Timestamp', new Date().toISOString());
res.set('FT-System-Code', meta.systemCode);
next();
}
);
// metrics should be one of the first things as needs to be applied before any other middleware executes
metrics.init({
flushEvery: 40000
});
app.use(
/** @type {Callback} */ (req, res, next) => {
metrics.instrument(req, { as: 'express.http.req' });
metrics.instrument(res, { as: 'express.http.res' });
next();
}
);
if (options.withServiceMetrics) {
metrics.fetch.instrument();
}
if (options.withBackendAuthentication) {
backendAuthentication(app);
}
// feature flags
if (options.withFlags) {
addInitPromise(flags.init());
app.use(flags.middleware);
}
// consent preference flags
if (options.withConsent) {
app.use(consentMiddleware);
}
// cache-control constants
app.use(cache);
if (options.logVary) {
app.use(logVary);
}
if (options.withAnonMiddleware) {
app.use(anon.middleware);
}
return { app, meta, addInitPromise };
};
/**
* @param {AppOptions} options
* @returns {Application}
*/
module.exports = (options) => getAppContainer(options).app;
// expose internals the app may want access to
module.exports.json = express.json;
module.exports.text = express.text;
module.exports.urlencoded = express.urlencoded;
module.exports.Router = express.Router;
module.exports.static = express.static;
module.exports.metrics = metrics;
module.exports.flags = flags;
module.exports.getAppContainer = getAppContainer;