2023-10-26 08:29:45 +03:00
|
|
|
const
|
|
|
|
express = require('express'),
|
2023-10-25 20:38:34 +03:00
|
|
|
app = module.exports = express(),
|
|
|
|
server = require('http').createServer(app),
|
2012-06-08 17:06:54 +03:00
|
|
|
Stopwatch = require('./models/stopwatch'),
|
2023-10-25 20:38:34 +03:00
|
|
|
errorHandler = require('errorhandler'),
|
2024-10-27 14:47:03 +02:00
|
|
|
expressLayouts = require('express-ejs-layouts');
|
|
|
|
|
|
|
|
const auth = (req, res, next) => {
|
|
|
|
const authHeader = req.headers.authorization;
|
|
|
|
|
|
|
|
if (!authHeader) {
|
|
|
|
return res.set("WWW-Authenticate", "Basic realm='timer'").status(401).send('Unauthorized');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const authTokens = authHeader.split(' ');
|
|
|
|
if (authTokens[0] === 'Basic') {
|
|
|
|
const credentials = Buffer.from(authTokens[1], 'base64').toString('utf8');
|
|
|
|
|
|
|
|
if (credentials !== process.env.BASIC_AUTH) { // this is obviously not the most secure way, but we don't care
|
|
|
|
return res.status(401).send('Unauthorized');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return res.status(401).send('Unauthorized');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-06-05 08:45:50 +03:00
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
if (process.env.BASIC_AUTH) {
|
|
|
|
app.use(auth);
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:38:34 +03:00
|
|
|
app.set('views', __dirname + '/views');
|
|
|
|
app.set('view engine', 'ejs');
|
2012-06-05 08:45:50 +03:00
|
|
|
|
2023-10-25 20:38:34 +03:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
|
|
|
|
}
|
2012-06-05 08:45:50 +03:00
|
|
|
|
2023-10-25 20:38:34 +03:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
app.use(errorHandler());
|
|
|
|
}
|
2012-06-05 08:54:10 +03:00
|
|
|
|
2012-06-05 08:45:50 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
// Use the port and host
|
2012-06-08 17:06:54 +03:00
|
|
|
var port = process.env.PORT || 5000;
|
2022-10-12 22:53:16 +03:00
|
|
|
var host = process.env.HOST || '0.0.0.0';
|
2012-06-05 08:45:50 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
var prefix = process.env.PREFIX || '';
|
|
|
|
|
|
|
|
const halls = (process.env.HALLS || ((process.env.PREFIX || "") + ":" + (process.env.TITLE || "TIMER"))).replace("; ?$", "").split(';').map(tuple => {
|
|
|
|
const parts = tuple.split(':');
|
|
|
|
return {"prefix": parts[0].trim(), "title": parts[1].trim()};
|
2023-10-26 08:29:45 +03:00
|
|
|
});
|
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
console.log("Configuring halls:", halls)
|
2023-10-26 08:29:45 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
halls.map(hall => {
|
|
|
|
const title = hall["title"]
|
|
|
|
const prefix = hall["prefix"]
|
|
|
|
const router = express.Router();
|
2012-06-08 17:06:54 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
const io = require('socket.io')(server, {path: prefix + '/socket.io'})
|
2012-06-08 17:06:54 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
router.use(expressLayouts);
|
|
|
|
router.use(express.static(__dirname + '/public'));
|
2012-06-05 08:45:50 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
var stopwatch = new Stopwatch();
|
2012-06-08 17:06:54 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
stopwatch.on('tick:stopwatch', function(time) {
|
|
|
|
io.sockets.emit('time', { time: time });
|
2023-10-26 08:29:45 +03:00
|
|
|
});
|
2012-06-08 17:06:54 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
stopwatch.on('reset:stopwatch', function(time) {
|
|
|
|
io.sockets.emit('time', { time: time });
|
2023-10-26 08:29:45 +03:00
|
|
|
});
|
2022-10-12 22:53:16 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
// stopwatch.start(); // we probably don't want autostart on server start
|
2022-10-12 22:53:16 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
io.sockets.on('connection', function (socket) {
|
|
|
|
io.sockets.emit('time', { time: stopwatch.getTime() });
|
2023-10-25 20:38:34 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
socket.on('click:start', function () {
|
|
|
|
stopwatch.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('click:stop', function () {
|
|
|
|
stopwatch.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('click:zero', function () {
|
|
|
|
stopwatch.zero();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('click:reset', function () {
|
|
|
|
stopwatch.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('click:resetShort', function () {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
});
|
2023-10-25 20:38:34 +03:00
|
|
|
|
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
router.get('/', function(req, res) {
|
|
|
|
res.render('index', { title: title, prefix: prefix });
|
|
|
|
});
|
2023-10-25 20:38:34 +03:00
|
|
|
|
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
const control = express.Router();
|
|
|
|
|
|
|
|
control.get('/', function(req, res) {
|
|
|
|
res.render('control', { title: title, prefix: prefix });
|
|
|
|
});
|
|
|
|
control.post('/reset/', function (req, res) {
|
|
|
|
stopwatch.reset();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
control.post('/reset-short/', function (req, res) {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
control.post('/start-from-reset/', function (req, res) {
|
|
|
|
stopwatch.reset();
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
control.post('/start-from-reset-short/', function (req, res) {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
control.post('/start/', function (req, res) {
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
control.post('/stop/', function (req, res) {
|
|
|
|
stopwatch.stop();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
control.post('/zero/', function (req, res) {
|
|
|
|
stopwatch.zero();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
router.use('/control', control);
|
2023-10-26 08:29:45 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
app.use(prefix, router);
|
2022-10-12 22:53:16 +03:00
|
|
|
});
|
2024-10-27 14:47:03 +02:00
|
|
|
|
|
|
|
server.listen(port, host, function() {
|
|
|
|
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
|
2022-10-12 22:53:16 +03:00
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
|
2024-10-27 14:47:03 +02:00
|
|
|
if (halls.length != 1 && halls[0]["prefix"] != "") {
|
|
|
|
app.get('/', function(req, res) {
|
|
|
|
res.render("list", { layout: false, halls: halls });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|