-
Notifications
You must be signed in to change notification settings - Fork 218
/
app.js
executable file
·60 lines (55 loc) · 1.53 KB
/
app.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
const https = require('https');
const fs = require('fs');
const config = require('./config');
const app = require('./lib/endpoints');
const eventBus = require('./lib/eventBus');
/**
* Naming:
* sid: Group of files
* key: File
* fid: {sid}++{key}
*/
let server;
if(config.port) {
// HTTP Server
server = app.listen(config.port, config.iface, () => {
console.log(`PsiTransfer listening on http://${config.iface}:${config.port}`);
eventBus.emit('listen', server);
});
}
let httpsServer;
if(config.sslPort && config.sslKeyFile && config.sslCertFile) {
// HTTPS Server
const sslOpts = {
key: fs.readFileSync(config.sslKeyFile),
cert: fs.readFileSync(config.sslCertFile)
};
httpsServer = https.createServer(sslOpts, app)
.listen(config.sslPort, config.iface, () => {
console.log(`PsiTransfer listening on https://${config.iface}:${config.sslPort}`);
eventBus.emit('listen', httpsServer);
});
}
// graceful shutdown
function shutdown() {
console.log('PsiTransfer shutting down...');
eventBus.emit('shutdown', server || httpsServer);
if(server) {
server.close(() => {
server = false;
if(!server && !httpsServer) process.exit(0);
});
}
if(httpsServer) {
httpsServer.close(() => {
httpsServer = false;
if(!server && !httpsServer) process.exit(0);
});
}
setTimeout(function() {
console.log('Could not close connections in time, forcefully shutting down');
process.exit(1);
}, 15 * 1000);
}
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);