forked from nextstrain/nextstrain.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
179 lines (145 loc) · 5.59 KB
/
server.js
File metadata and controls
179 lines (145 loc) · 5.59 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
179
/* eslint no-console: off */
const path = require("path");
const sslRedirect = require('heroku-ssl-redirect');
const nakedRedirect = require('express-naked-redirect');
const express = require("express");
const expressStaticGzip = require("express-static-gzip");
const favicon = require('serve-favicon');
const compression = require('compression');
const argparse = require('argparse');
const utils = require("./src/utils");
const production = process.env.NODE_ENV === "production";
const version = utils.getGitHash();
const nextstrainAbout = `
Nextstrain is an open-source project to harness the scientific and public health potential
of pathogen genome data. This is the server behind nextstrain.org.
It delivers the static content (https://github.com/nextstrain/static) as well as the interactive
visualisation app auspice (https://github.com/nextstrain/auspice), with customisations.
`;
const parser = new argparse.ArgumentParser({
version,
addHelp: true,
description: `Nextstrain.org server version ${version}`,
epilog: nextstrainAbout
});
parser.addArgument('--verbose', {action: "storeTrue", help: "verbose server logging"});
const args = parser.parseArgs();
global.verbose = args.verbose;
// Import these after parsing CLI arguments and setting global.verbose so code
// in them can use utils.verbose() at load time.
const auspiceServerHandlers = require("./src/index.js");
const authn = require("./authn");
const sources = require("./src/sources");
const redirects = require("./redirects");
/* Path helpers for static assets, to make routes more readable.
*/
const relativePath = (...subpath) =>
path.join(__dirname, ...subpath);
const gatsbyAssetPath = (...subpath) =>
relativePath("static-site", "public", ...subpath);
const auspiceAssetPath = (...subpath) =>
relativePath("auspice-client", ...subpath);
/* BASIC APP SETUP */
// NOTE: order of app.get is first come first serve (https://stackoverflow.com/questions/32603818/order-of-router-precedence-in-express-js)
const app = express();
// In production, trust Heroku as a reverse proxy and Express will use request
// metadata from the proxy.
if (production) app.enable("trust proxy");
app.set('port', process.env.PORT || 5000);
app.use(sslRedirect()); // redirect HTTP to HTTPS
app.use(compression()); // send files (e.g. res.json()) using compression (if possible)
app.use(nakedRedirect({reverse: true})); // redirect www.nextstrain.org to nextstrain.org
app.use(favicon(relativePath("favicon.ico")));
app.use('/favicon.png', express.static(relativePath("favicon.png")));
/* Authentication (authn)
*/
authn.setup(app);
/* Redirects.
*/
redirects.setup(app);
/* Static assets. Auspice hardcodes /dist/… in its Webpack config.
*/
app.use(express.static(gatsbyAssetPath()));
app.route("/dist/*")
.all(expressStaticGzip(auspiceAssetPath()));
/* Charon API used by Auspice.
*/
app.route("/charon/getAvailable")
.get(auspiceServerHandlers.getAvailable);
app.route("/charon/getDataset")
.get(auspiceServerHandlers.getDataset);
app.route("/charon/getNarrative")
.get(auspiceServerHandlers.getNarrative);
app.route("/charon/getSourceInfo")
.get(auspiceServerHandlers.getSourceInfo);
app.route("/charon/*")
.all((req, res) => res.status(404).end());
/* Dataset and narrative paths hit Auspice's entrypoint.
*/
const coreBuilds = [
"/dengue",
"/ebola",
"/enterovirus",
"/flu",
"/lassa",
"/measles",
"/mers",
"/mumps",
"/ncov",
"/tb",
"/WNV",
"/yellow-fever",
"/zika",
];
const groups = [...sources.keys()].filter((k) => !["core", "staging", "community"].includes(k));
const auspicePaths = [
"/status",
"/status/*",
...coreBuilds,
...coreBuilds.map((url) => url + ":*"),
...coreBuilds.map(url => url + "/*"),
"/narratives",
"/narratives/*",
"/staging",
"/staging/*",
...groups.map((group) => `/groups/${group}`),
...groups.map((group) => `/groups/${group}/*`),
/* Auspice gets specific /community paths so it can show an index of datasets
* and narratives, but Gatsby gets top-level /community.
*/
"/community/:user/:repo",
"/community/:user/:repo/*",
];
app.route(auspicePaths).get((req, res) => {
utils.verbose(`Sending Auspice entrypoint for ${req.originalUrl}`);
res.sendFile(auspiceAssetPath("index.html"));
});
/* handle redirects for inrb-drc (first of the Nextstrain groups) */
app.get("/inrb-drc*", (req, res) => {
res.redirect(`/groups${req.originalUrl}`);
});
/* Everything else hits our Gatsby app's entrypoint.
*/
app.get("*", (req, res) => {
utils.verbose(`Sending Gatsby entrypoint for ${req.originalUrl}`);
res.sendFile(gatsbyAssetPath(""), {}, (err) => {
// This error callback is used to display the 404 page
if (err.code === 'EISDIR') res.status(404).sendFile(gatsbyAssetPath("404.html"));
});
});
const server = app.listen(app.get('port'), () => {
console.log("-----------------------------------");
console.log(nextstrainAbout);
console.log(` Server listening on port ${server.address().port}`);
console.log(` Accessible at https://nextstrain.org or http://localhost:${server.address().port}`);
console.log(` Auspice datasets are sourced from S3 buckets.`);
console.log("\n-----------------------------------\n\n");
}).on('error', (err) => {
if (err.code === 'EADDRINUSE') {
utils.error(`Port ${app.get('port')} is currently in use by another program.
You must either close that program or specify a different port by setting the shell variable
"$PORT". Note that on MacOS / Linux, "lsof -n -i :${app.get('port')} | grep LISTEN" should
identify the process currently using the port.`);
}
utils.error(`Uncaught error in app.listen(). Code: ${err.code}`);
});