-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
141 lines (122 loc) · 4.38 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var colors = require('colors');
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var fs = require('fs');
var path = require('path');
var neo4j = require('neo4j-driver');
var jwt = require('jsonwebtoken');
var config = require('dotenv').config();
var multipart = require('connect-multiparty');
// Connect to Neo4j
exports.driver = neo4j.driver(
"bolt://" + process.env.NEO4J_HOST + ":" + process.env.NEO4J_PORT,
neo4j.auth.basic( process.env.NEO4J_USERNAME, process.env.NEO4J_PASSWORD),
// { connectionTimeout: 3000 },
)
exports.driver.verifyConnectivity()
.then(function(result) {
console.log(colors.green(new Date() + " Neo4j is running on port " + process.env.NEO4J_PORT));
})
.catch(function(err) {
console.error(colors.red(new Date() + " Neo4j could not be accessed:\n" + JSON.stringify(err)));
});
// Load certificates
if (process.env.NODE_ENV === "production") {
var privateKey = fs.readFileSync('ssl/server.key', 'utf8');
var certificate = fs.readFileSync('ssl/server.crt', 'utf8');
var credentials = {
key: privateKey,
cert: certificate
};
}
// Server settings
var app = express();
app.use(bodyParser.json({
limit: 52428800 // 50MB
}));
app.use(bodyParser.urlencoded({
extended: false,
limit: 52428800 // 50MB
}));
// Set folder for static files
app.use(express.static(__dirname + '/public', {
redirect: true
}));
// Allow CORS, disable caching for API
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Cache-Control", "no-store");
next();
});
app.use(multipart({
uploadDir: config.tmp
}));
// Authentication
exports.isAuthenticated = function isAuthenticated(req, res, next) {
if (req.headers.authorization) {
var token = req.headers.authorization.substring(7);
// Verify token
jwt.verify(token, process.env.JWT_SECRET, function(err, decoded) {
if(err){
res.status(401).send("Authentication failed!");
} else {
// Authorization
if(decoded.username === process.env.ADMIN_USERNAME && decoded.iss === process.env.SERVER_URL){
return next();
} else {
res.status(401).send("Authentication failed!");
}
}
});
} else {
res.status(401).send("Authentication failed!");
}
};
// API endpoint
var prefix = '/api';
// Load API routes
app.use(prefix, require('./routes/reset'));
app.use(prefix, require('./routes/login'));
app.use(prefix, require('./routes/scenarios'));
app.use(prefix, require('./routes/locations'));
app.use(prefix, require('./routes/videos'));
app.use(prefix, require('./routes/overlays'));
app.use(prefix, require('./routes/relationships'));
app.use(prefix, require('./routes/search'));
app.use(prefix, require('./routes/handlers'));
var cms = require('./routes/cms');
app.use(cms);
// Resolve path after refreshing inside app
app.get('/', function(req, res, next) {
res.sendFile(path.resolve('public/index.html'));
});
app.get('/creator/*', function(req, res, next) {
res.sendFile(path.resolve('public/creator/index.html'));
});
app.get('/cms/*', function(req, res, next) {
res.sendFile(path.resolve('public/cms/index.html'));
});
app.get('/viewer/*', function(req, res, next) {
res.sendFile(path.resolve('public/viewer/index.html'));
});
app.get('/remote/*', function(req, res, next) {
res.sendFile(path.resolve('public/remote/index.html'));
});
// Start Webserver
var httpServer = http.createServer(app);
httpServer.listen(Number(process.env.HTTP_PORT), function() {
console.log(colors.green(new Date() + " HTTP-Server is listening at port " + process.env.HTTP_PORT));
});
if(process.env.NODE_ENV === "production") {
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(Number(process.env.HTTPS_PORT), function() {
console.log(colors.green(new Date() + " HTTPS-Server is listening at port " + process.env.HTTPS_PORT));
});
}
// Start websocket server
var io = require('socket.io')(httpServer);
exports.io = io;
require('./controllers/sockets.js').sockets;
console.log(colors.green(new Date() + " Websocket-Server is listening at port " + process.env.HTTP_PORT));