-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
124 lines (101 loc) · 2.69 KB
/
server.js
File metadata and controls
124 lines (101 loc) · 2.69 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
/** ****************************************************************************************************
* File: index.js
* Project: basic-fs
* @author Nick Soggin <iSkore@users.noreply.github.com> on 11-Jul-2017
*******************************************************************************************************/
'use strict';
const
gonfig = require( 'gonfig' ),
http = require( 'http' ),
express = require( 'express' ),
bodyParser = require( 'body-parser' ),
{ resolve } = require( 'path' ),
debug = require( './debug' );
let isClosed = false;
class BasicFS
{
constructor()
{
}
hookRoute( item )
{
item.exec = require( resolve( item.exec ) );
this.express[ item.method.toLowerCase() ](
item.route,
( req, res ) => res ?
item.exec( req, res ) :
res.status( 500 ).send( 'unknown' )
);
return item;
}
expressInitialize()
{
this.express = express();
this.express.disable( 'x-powered-by' );
this.express.use( bodyParser.raw( { limit: '5gb' } ) );
this.express.use( bodyParser.json() );
this.express.use( require( './lib/inspection' )() );
gonfig.set( 'api', gonfig.get( 'api' ).map( item => this.hookRoute( item ) ) );
this.express.use( require( './lib/captureErrors' )() );
}
initialize()
{
this.expressInitialize();
process
.on( 'uncaughtException', err => debug( gonfig.getErrorReport( err ) ) )
.on( 'unhandledRejection', err => debug( gonfig.getErrorReport( err ) ) )
.on( 'SIGINT', () => {
debug( 'Received SIGINT, graceful shutdown...' );
this.shutdown( 0 );
} )
.on( 'exit', code => {
debug( `Received exit with code ${ code }, graceful shutdown...` );
this.shutdown( code );
} );
return this;
}
start()
{
return new Promise(
res => {
this.server = http.createServer( this.express );
this.server.listen(
gonfig.get( 'server' ).port,
() => {
debug(
`${ gonfig.get( 'name' ) } ` +
`v${ gonfig.get( 'version' ) } ` +
`running on ${ gonfig.get( 'lanip' ) }:${ gonfig.get( 'server' ).port }`
);
res( this );
}
);
}
);
}
shutdown( code = 0 )
{
if( this.server ) {
this.server.close();
}
if( isClosed ) {
debug( 'Shutdown after SIGINT, forced shutdown...' );
process.exit( 0 );
}
isClosed = true;
debug( `server exiting with code: ${ code }` );
process.exit( code );
}
}
module.exports = new BasicFS();
// basicauth = require( 'express-basic-auth' ),
// if( process.config.authentication === 'basicauth' ) {
// const { challenge, realm } = process.config;
//
// this.express.use( basicauth( {
// authorizeAsync: true,
// authorizer: this.authorizer,
// challenge,
// realm
// } ) );
// }