This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.tsx
132 lines (111 loc) · 4.03 KB
/
index.tsx
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
/* tslint:disable:no-console */
import path from 'path';
import Koa from 'koa';
import Raven from 'raven';
import serve from 'koa-static';
import mount from 'koa-mount';
import compress from 'koa-compress';
import bodyParser from 'koa-bodyparser';
import stoppable from 'stoppable';
import nullthrows from 'nullthrows';
import sw from './sw';
import routes from './routes';
import { AddressInfo } from 'net';
type ShutdownSignal = 'SIGHUP' | 'SIGINT' | 'SIGTERM' | 'SIGUSR2';
const port = parseInt(process.env.SNACK_PORT || '', 10) || 3011;
const host = '::';
const backlog = 511;
const timeout = 30000;
if (require.main === module) {
if (process.env.NODE_ENV === 'development') {
require('source-map-support').install();
}
Raven.config(nullthrows(process.env.SNACK_SENTRY_DSN), {
release:
process.env.NODE_ENV === 'production' ? nullthrows(process.env.APP_VERSION) : undefined,
captureUnhandledRejections: true,
shouldSendCallback() {
return process.env.NODE_ENV === 'production';
},
}).install();
}
const app = new Koa();
if (process.env.COMPRESS_ASSETS === 'true') {
// Enable gzip compression conditionally, for development
// This makes it easier to test how big bundles will be in production
app.use(compress());
}
app.use(sw());
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') {
app.use(mount('/dist', serve(path.join(__dirname, '..', '..', 'dist'))));
} else {
// Use webpack dev middleware in development
const webpack = require('webpack');
const dev = require('webpack-dev-middleware');
const config = require('../../webpack.config');
const compiler = webpack(config);
const middleware = dev(compiler, {
publicPath: '/dist/',
stats: 'minimal',
});
app.use(async (ctx, next) => {
await middleware(
ctx.req,
{
end: (content: string) => {
ctx.body = content;
},
setHeader: (name: string, value: string) => {
ctx.set(name, value);
},
},
next
);
});
}
app.use(bodyParser());
app.use(routes());
const httpServer = app.listen(port, host, backlog, () => {
const { address, port } = server.address() as AddressInfo;
console.log(`The Snack web server is listening on http://${address}:${port}`);
});
httpServer.timeout = timeout;
// In development, it's common to stop or restart the server so we immediately end and close all
// sockets when stopping the server instead of waiting for the requests to finish. In production,
// we allow the requests a grace period to complete before ending and closing the sockets.
const gracePeriod = process.env.NODE_ENV === 'development' ? 0 : httpServer.timeout;
const server = stoppable(httpServer, gracePeriod);
// Listen to HTTP server error events and handle shutting down the server gracefully
let exitSignal: ShutdownSignal | null = null;
let httpServerError: Error | null = null;
server.on('error', error => {
httpServerError = error;
console.error(`There was an error with the HTTP server:`, error);
console.error(`The HTTP server is shutting down and draining existing connections`);
server.stop();
});
server.on('close', () => {
console.log(`The HTTP server has drained all connections and is scheduling its exit`);
console.log(`The HTTP server process is exiting...`);
// Let other "close" event handlers run before exiting
process.nextTick(() => {
if (exitSignal) {
process.kill(process.pid, exitSignal);
} else {
process.exit(httpServerError ? 1 : 0);
}
});
});
const shutdown = (signal: ShutdownSignal) => {
console.log(
`Received ${signal}; the HTTP server is shutting down and draining existing connections`
);
exitSignal = signal;
server.stop();
};
// TODO: In Node 9, the signal is passed as the first argument to the listener
process.once('SIGHUP', () => shutdown('SIGHUP'));
process.once('SIGINT', () => shutdown('SIGINT'));
process.once('SIGTERM', () => shutdown('SIGTERM'));
// Nodemon sends SIGUSR2 when it restarts the process it's monitoring
process.once('SIGUSR2', () => shutdown('SIGUSR2'));