-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
96 lines (79 loc) · 2.71 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
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
'use strict';
const path = require('path');
const GameController = require('./app/controllers/game-controller');
const VideoController = require('./app/controllers/video-controller');
const express = require('express');
const auth = require('http-auth');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const favicon = require('serve-favicon');
const lessMiddleware = require('less-middleware');
const DbService = require('./app/services/db-service');
const { ADMIN_USER, ADMIN_PWD } = process.env;
const basic = auth.basic({
realm: 'acnwall',
}, (username, password, callback) => {
callback(username === ADMIN_USER && password === ADMIN_PWD);
});
const authMiddleware = auth.connect(basic);
app.use(lessMiddleware(path.join(__dirname, 'public')));
// Expose all static resources in /public
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'favicon.png')));
// Redirect to the main page
app.get('/spectate', (request, response) => {
response.sendFile('game.html', {
root: path.join(__dirname, 'app/views'),
});
});
app.get('/', (req, res) => {
res.sendFile('play.html', {
root: path.join(__dirname, 'app/views'),
});
});
app.get('/videos', (req, res) => {
res.sendFile('video.html', { root: path.join(__dirname, 'app/views') });
});
app.get('/draw', (req, res) => {
res.sendFile('draw.html', { root: path.join(__dirname, 'app/views') });
});
app.get('/admin', authMiddleware, (req, res) => {
res.sendFile('videoadmin.html', { root: path.join(__dirname, 'app/views') });
});
const videoController = new VideoController();
videoController.listen(io);
app.post('/videos', authMiddleware, (req, res) => {
videoController.startVideos();
res.redirect('/admin');
});
app.post('/game', authMiddleware, (req, res) => {
videoController.startGame();
res.redirect('/admin');
});
// Create the main controller
const gameController = new GameController();
gameController.listen(io);
app.get('/settings', (req, res) => {
res.sendFile('settings.html', { root: path.join(__dirname, 'app/views') });
});
app.get('/users/:playerName', (req, res) => {
DbService.getPlayer(req.params.playerName)
.then(() => {
res.send({
available: false,
});
})
.catch(() => {
res.send({
available: true,
});
});
});
const SERVER_PORT = process.env.PORT || 3000;
app.set('port', SERVER_PORT);
// Start Express server
server.listen(app.get('port'), () => {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
module.exports = app;