-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-server.js
59 lines (47 loc) · 1.52 KB
/
create-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
module.exports = function createServer(createGame, onJoinGame, onGameAction) {
const express = require('express');
const app = express();
const compression = require('compression');
const bodyParser = require('body-parser');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const Sentry = require('@sentry/node');
const SentryTracing = require('@sentry/tracing');
const fs = require('fs');
const path = require('path');
const PORT = 80;
Sentry.init({
dsn: process.env.SENTRY_DSN_BACKEND,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new SentryTracing.Integrations.Express({ app }),
],
release: process.env.COMMIT_HASH,
});
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(compression());
const mixManifest = require(path.join(__dirname, 'public', 'mix-manifest.json'));
app.get('/', (req, res) => {
fs.readFile(path.join(__dirname, 'public', 'index.html'), (error, data) => {
if(error) {
res.sendStatus(500);
throw error;
} else {
res.send(data.toString().replace(/ASSET\[([^]+?)\]/g, ($0, $1) => mixManifest[$1]));
}
});
});
app.use(express.static('public', { index: false, maxAge: '1y' }));
app.use(bodyParser.json());
app.post('/game', createGame);
io.on('connection', function (socket) {
onJoinGame(socket);
socket.on('game-action', function (data) {
onGameAction(socket, data);
});
});
server.listen(PORT);
app.use(Sentry.Handlers.errorHandler());
return io;
};