-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (53 loc) · 1.58 KB
/
server.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
'use strict';
const { envBool } = require( './lib/util' );
const maybeEnableMemoryDebugger = require( './lib/debug/memory-debugger' );
maybeEnableMemoryDebugger();
const server = require( 'fastify' )( {
logger: envBool( 'DEBUG_QUIET_REQUEST' ) ? false : true,
maxParamLength: 50000, // this defaults to 100, which is way too small
} );
if ( envBool( 'DEBUG_QUIET_REQUEST' ) ) {
console.debug( 'Quiet mode enabled.' );
}
if ( envBool( 'MINIFIERS_DISABLE_COMPRESSION' ) ) {
console.debug( 'Compression disabled.' );
}
// Routes
server.get( '/', require( './routes/index' ) );
server.get( '/health-check', require( './routes/health-check' ) );
server.get( '/file', require( './routes/file' ) );
server.get( '/get', require( './routes/get' ) );
server.options( '/get', require( './routes/get' ) );
// Take care of command line options
const opt = require( 'node-getopt' )
.create( [ [ 'p', 'port=4747', 'The TCP port that the web server will listen on.', 4747 ] ] )
.bindHelp()
.parseSystem();
// Handle shutdown signals
[ 'SIGINT', 'SIGTERM' ].forEach( ( signal ) => {
process.on( signal, () => {
console.log( `Received ${ signal }, shutting down...` );
server.close( ( err ) => {
if ( err ) {
console.error( 'Error during shutdown:', err );
process.exit( 1 );
}
console.log( 'Server closed' );
process.exit( 0 );
} );
} );
} );
// Run the server
server.listen(
{
port: opt.options.port,
host: '0.0.0.0',
},
( err, address ) => {
// If this happens something very bad has happened, end of the world.
if ( err ) {
console.log( err );
process.exit( 1 );
}
},
);