-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.ts
More file actions
178 lines (159 loc) · 6.11 KB
/
server.ts
File metadata and controls
178 lines (159 loc) · 6.11 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
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
177
178
import compression from 'compression';
import cors from 'cors';
import type { Express } from 'express';
import express from 'express';
import { buildAuxiaProxyRouter, getAuxiaRouterConfig } from './api/auxiaProxyRouter';
import { buildBannerRouter } from './api/bannerRouter';
import { buildEpicRouter } from './api/epicRouter';
import { buildGutterRouter } from './api/gutterRouter';
import { buildHeaderRouter } from './api/headerRouter';
import { buildTickerRouter } from './api/tickerRouter';
import { buildChannelSwitchesReloader } from './channelSwitches';
import { buildChoiceCardAmountsReloader } from './choiceCardAmounts';
import { buildTickerDataReloader } from './lib/fetchTickerData';
import { getMParticleConfig, MParticle } from './lib/mParticle';
import { getOktaConfig, Okta } from './lib/okta';
import { buildPromotionsReloader } from './lib/promotions/promotions';
import { buildSuperModeArticlesReloader } from './lib/superMode';
import {
errorHandling as errorHandlingMiddleware,
logging as loggingMiddleware,
} from './middleware';
import { buildProductCatalogReloader } from './productCatalog';
import { buildBanditDataReloader } from './selection/banditData';
import { buildBannerDeployTimesReloader } from './tests/banners/bannerDeployTimes';
import { buildBannerDesignsReloader } from './tests/banners/bannerDesigns';
import { buildBannerTestsReloader } from './tests/banners/bannerTests';
import { buildEpicLiveblogTestsReloader, buildEpicTestsReloader } from './tests/epics/epicTests';
import { buildGutterLiveblogTestsReloader } from './tests/gutters/gutterTests';
import { buildHeaderTestsReloader } from './tests/headers/headerTests';
import { logError } from './utils/logging';
const buildApp = async (): Promise<Express> => {
const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(compression());
const dotcomDevOrigins = [
// see: https://github.com/guardian/dotcom-rendering/blob/f05969110b0ab9af18041bbe537f93f98d28ad8f/dotcom-rendering/scripts/nginx/setup.sh#L11
'https://r.thegulocal.com',
'http://localhost:3030',
'http://localhost:9000',
];
const corsOrigin = () => {
switch (process.env.stage) {
case 'PROD':
return [
'https://www.theguardian.com',
'https://support.gutools.co.uk',
'https://support.code.dev-gutools.co.uk',
...dotcomDevOrigins,
];
case 'CODE':
return [
'https://m.code.dev-theguardian.com',
'https://support.gutools.co.uk',
'https://support.code.dev-gutools.co.uk',
...dotcomDevOrigins,
];
default:
return '*';
}
};
const corsOptions = {
origin: corsOrigin(),
};
app.use(cors(corsOptions));
app.use(loggingMiddleware);
app.use(express.urlencoded({ extended: true }));
const stage =
process.env.stage === 'CODE' ? 'CODE' : process.env.stage === 'DEV' ? 'DEV' : 'PROD';
// Initialise dependencies
const [
channelSwitches,
superModeArticles,
articleEpicTests,
liveblogEpicTests,
choiceCardAmounts,
tickerData,
bannerTests,
bannerDeployTimes,
headerTests,
bannerDesigns,
gutterLiveblogTests,
productCatalog,
promotions,
] = await Promise.all([
buildChannelSwitchesReloader(),
buildSuperModeArticlesReloader(),
buildEpicTestsReloader(),
buildEpicLiveblogTestsReloader(),
buildChoiceCardAmountsReloader(),
buildTickerDataReloader(stage),
buildBannerTestsReloader(),
buildBannerDeployTimesReloader(),
buildHeaderTestsReloader(),
buildBannerDesignsReloader(),
buildGutterLiveblogTestsReloader(),
buildProductCatalogReloader(),
buildPromotionsReloader(),
]);
const banditData = await buildBanditDataReloader(articleEpicTests, bannerTests);
const auxiaConfig = await getAuxiaRouterConfig();
const mParticleConfig = await getMParticleConfig();
const mParticle = new MParticle(mParticleConfig);
const oktaConfig = await getOktaConfig();
const okta = new Okta(oktaConfig);
// Build the routers
app.use(
buildEpicRouter(
channelSwitches,
superModeArticles,
articleEpicTests,
liveblogEpicTests,
choiceCardAmounts,
tickerData,
banditData,
productCatalog,
promotions,
mParticle,
okta,
),
);
app.use(
buildBannerRouter(
channelSwitches,
tickerData,
bannerTests,
bannerDeployTimes,
choiceCardAmounts,
bannerDesigns,
banditData,
productCatalog,
promotions,
mParticle,
okta,
),
);
app.use(buildHeaderRouter(channelSwitches, headerTests));
app.use(buildAuxiaProxyRouter(channelSwitches, auxiaConfig));
app.use(buildGutterRouter(channelSwitches, gutterLiveblogTests));
app.use(buildTickerRouter(tickerData));
// The error handling middleware must be the last middleware in the chain
// https://stackoverflow.com/questions/72227296/does-the-position-of-error-handling-middleware-matter-in-express
app.use(errorHandlingMiddleware);
app.get('/healthcheck', (req: express.Request, res: express.Response) => {
res.header('Content-Type', 'text/plain');
res.send('OK');
});
return Promise.resolve(app);
};
buildApp()
.then((app) => {
const PORT = process.env.PORT ?? 3030;
const server = app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
// keep-alive timeout should match LB idle timeout value, see https://repost.aws/knowledge-center/elb-alb-troubleshoot-502-errors
server.keepAliveTimeout = 60000;
})
.catch((err) => {
logError(`Failed to start server: ${String(err)}`);
throw err;
});