-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
58 lines (50 loc) · 2.05 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
var express = require('express');
var app = express();
var Config = require('./config')()
var mongoose = require('mongoose');
var Utilities = require("./utilities")()
var flags = require("node-flags")
var path = require('path')
// handle the --debug parameter
Config.setWebcam(!flags.get("debug"));
// Support for JSON-encoded POST parameters
app.use(express.json({limit : '50mb'}))
// Connect to mongo server
const options = {
reconnectTries: 5, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0
};
mongoose.connect(Config.getMongoServer(), options).then(
() => { Utilities.log("Connection to MongoDB established successfully", "info"); },
error => { Utilities.log("Connection to MongoDB could not be established. Error :\n" + error.toString(), "info"); }
);
// Handle all routes starting with /storyface
var routes = require("./routes")(Config, Utilities)
app.use('/storyface', routes)
// Routes that do not start with /storyface
app.use(function(req, res) {
Utilities.log("Unhandled non-Storyface page request : " + req.originalUrl);
res.status(404).send('Not found');
})
if( Config.getEnvironment() == "dev")
{ // In development environment the the server is an HTTPS server
var fs = require('fs');
var https = require('https');
var privateKey = fs.readFileSync(path.join(__dirname, path.join('ssl_certificates','key.pem')), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, path.join('ssl_certificates','cert.pem')), 'utf8');
var credentials = {key: privateKey, cert: certificate};
var server = https.createServer(credentials, app);
}
else
{
var http = require("http");
server = http.createServer(app);
}
var listeningServer = server.listen(Config.getServerPort(), Config.getServerIP(), function () {
var host = listeningServer.address().address
var port = listeningServer.address().port
Utilities.log("Listening on https://" + host + ":" + port)
});