-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
158 lines (129 loc) · 3.63 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require('module-alias/register');
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const chalk = require('chalk');
const config = require('./config')();
const {authorize} = require('@b/utils');
require('dotenv').config();
// Announce environment
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'production') {
console.log('Running application for production');
}
else {
console.log('Running application for development');
}
// Set up Express.js
app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
const server = require('http').Server(app);
const io = require('socket.io')(server);
const PORT = process.env.PORT || config.port;
const DATABASE = process.env.MONGODB_URI || config.database.uri;
app.get('/favicon.(ico|png)', (req, res) => {
res.sendFile(`${__dirname}/src/frontend/favicon.png`);
});
app.get('/public/manifest.json', (req, res) => {
res.sendFile(`${__dirname}/src/frontend/manifest.json`);
});
// CORS
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
next();
});
// Handle errors
app.use(function(err, req, res, next) {
if (err instanceof SyntaxError) {
res.json({'error': 'Invalid JSON'});
}
else {
next();
}
});
// Catch all for backend API
app.use(require('./src/backend/routes')());
app.get('/index.html', (req, res) => {
res.redirect('/');
});
// Frontend endpoints
app.use('/public', express.static(`${__dirname}/dist`));
// Catch all for frontend routes
app.all('/*', function(req, res) {
res.sendFile(`${__dirname}/dist/index.html`);
});
// Server-side Socket.IO
io.on('connection', socket => {
let workspace = 'all';
let req = token => ({
headers: {
authorization: `token ${token}`
}
});
socket.on('join', (token) => {
authorize(req(token)).then(() => {
socket.join(workspace);
}).catch(err => {
});
});
socket.on('leave', () => {
socket.leave(workspace);
});
socket.on('updatedConfig', (token) => {
authorize(req(token)).then(() => {
io.sockets.in(workspace).emit('updateConfig');
}).catch(err => {
});
});
socket.on('updatedHackers', (token) => {
authorize(req(token)).then(() => {
io.sockets.in(workspace).emit('updateHackers');
}).catch(err => {
});
});
socket.on('updatedUsers', (token) => {
authorize(req(token)).then(() => {
io.sockets.in(workspace).emit('updateUsers');
}).catch(err => {
});
});
socket.on('updatedRoles', (token) => {
authorize(req(token)).then(() => {
io.sockets.in(workspace).emit('updateRoles');
}).catch(err => {
});
});
socket.on('updatedInteractions', (token) => {
authorize(req(token)).then(() => {
io.sockets.in(workspace).emit('updateInteractions');
}).catch(err => {
});
});
socket.on('disconnect', () => {
socket.leave(workspace);
});
});
server.listen(PORT);
console.log(chalk.green('Started on port ' + PORT));
mongoose.Promise = global.Promise;
mongoose.set('useFindAndModify', false); // Allows findOneAndUpdate()
const conn = () => {
mongoose.connect(DATABASE, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
};
conn();
const db = mongoose.connection;
db.on('error', err => {
console.log(chalk.red('Error connecting to MongoDB: ' + err));
console.log('Trying again...');
setTimeout(() => conn(), config.database.reconnectInterval);
});
db.once('open', () => {
console.log(chalk.green('Connected to MongoDB: ' + DATABASE));
});