-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
92 lines (68 loc) · 2.2 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
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
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const http = require('http');
const fs = require('fs');
const WebSocket = require('ws');
const CoffeLight = require('./app/app');
let config = {};
if (fs.existsSync(__dirname + "/config.json")) {
config = require("./config.json");
} else {
console.error("WARNING: Could not find config.json. Using default values");
}
global.coffeLight = new CoffeLight(config);
const routes = require('./app/routes');
const webSocketRoutes = require('./app/websocket');
const app = express();
// set our port
const port = app.locals.port = process.env.PORT || 8080;
// middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(session({
secret: coffeLight.config.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
//secure: true
}
}));
// routes
app.use('/api/v1/', routes);
app.get('/firebase.js', (req, res) => {
res.type('text/javascript').send("const firebaseConfig = " + JSON.stringify(coffeLight.config.firebase.web) + ";");
});
app.use(express.static(__dirname + '/public'));
// start app at localhost:8080
const server = http.createServer(app);
const wss = new WebSocket.Server({
server: server,
path: "/websocket"
});
wss.on('connection', webSocketRoutes);
server.listen(port, () => {
console.log(`Listening on ${port}`);
});
function shutdownHandler() {
console.log("Shutting down");
let timeout = setTimeout(() => {
console.error("Could not shutdown in time");
coffeLight.close().then(() => {
process.exit(1);
});
}, coffeLight.config.shutdownTimeout);
server.close(() => {
console.log("Server closed");
clearTimeout(timeout);
coffeLight.close().then(() => {
//console.log(process._getActiveHandles().filter(h=>h._type!=='tty'));
//console.log(process._getActiveRequests());
//process.exit(0);
});
});
}
process.on('SIGTERM', shutdownHandler);
process.on('SIGINT', shutdownHandler);