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'),
|
|
|
|
expressLayouts = require('express-ejs-layouts'),
|
|
|
|
io = require('socket.io')(server, {});
|
2012-06-05 08:45:50 +03:00
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
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
|
|
|
app.use(expressLayouts);
|
|
|
|
app.use(express.static(__dirname + '/public'));
|
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
|
|
|
|
2012-06-08 17:06:54 +03:00
|
|
|
// Use the port that Heroku provides or default to 5000
|
|
|
|
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
|
|
|
|
2023-10-25 20:38:34 +03:00
|
|
|
server.listen(port, host, function() {
|
2023-10-26 08:29:45 +03:00
|
|
|
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
|
|
|
|
});
|
|
|
|
|
2012-06-08 17:06:54 +03:00
|
|
|
var stopwatch = new Stopwatch();
|
2023-10-26 08:29:45 +03:00
|
|
|
|
2012-06-08 17:06:54 +03:00
|
|
|
stopwatch.on('tick:stopwatch', function(time) {
|
2023-10-26 08:29:45 +03:00
|
|
|
io.sockets.emit('time', { time: time });
|
|
|
|
});
|
2012-06-08 17:06:54 +03:00
|
|
|
|
|
|
|
stopwatch.on('reset:stopwatch', function(time) {
|
2023-10-26 08:29:45 +03:00
|
|
|
io.sockets.emit('time', { time: time });
|
|
|
|
});
|
2012-06-08 17:06:54 +03:00
|
|
|
|
|
|
|
stopwatch.start();
|
2012-06-05 08:45:50 +03:00
|
|
|
|
|
|
|
io.sockets.on('connection', function (socket) {
|
2023-10-26 08:29:45 +03:00
|
|
|
io.sockets.emit('time', { time: stopwatch.getTime() });
|
2012-06-08 17:06:54 +03:00
|
|
|
|
2023-10-26 08:29:45 +03:00
|
|
|
socket.on('click:start', function () {
|
|
|
|
stopwatch.start();
|
|
|
|
});
|
2012-06-08 17:06:54 +03:00
|
|
|
|
2023-10-26 08:29:45 +03:00
|
|
|
socket.on('click:stop', function () {
|
|
|
|
stopwatch.stop();
|
|
|
|
});
|
2022-10-12 22:53:16 +03:00
|
|
|
|
2023-10-26 08:29:45 +03:00
|
|
|
socket.on('click:zero', function () {
|
|
|
|
stopwatch.zero();
|
|
|
|
});
|
2022-10-12 22:53:16 +03:00
|
|
|
|
2023-10-26 08:29:45 +03:00
|
|
|
socket.on('click:reset', function () {
|
|
|
|
stopwatch.reset();
|
|
|
|
});
|
2023-10-25 20:38:34 +03:00
|
|
|
|
2023-10-26 08:29:45 +03:00
|
|
|
socket.on('click:resetShort', function () {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
});
|
2023-10-25 20:38:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// configure title
|
|
|
|
//app.use(function (req, res, next) {
|
|
|
|
// res.locals.title = process.env.TITLE || 'Timer'
|
|
|
|
//});
|
|
|
|
|
|
|
|
app.get('/', function(req, res) {
|
|
|
|
res.render('index', { title: process.env.TITLE });
|
|
|
|
});
|
|
|
|
|
2023-10-26 08:29:45 +03:00
|
|
|
|
|
|
|
const control = express.Router();
|
|
|
|
|
|
|
|
control.get('/', function(req, res) {
|
2023-10-25 20:38:34 +03:00
|
|
|
res.render('control', { title: process.env.TITLE });
|
2022-10-12 22:53:16 +03:00
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/reset/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.reset();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/reset-short/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.resetShort();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/start-from-reset/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.reset();
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/start-from-reset-short/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.resetShort();
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/start/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/stop/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.stop();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
control.post('/zero/', function (req, res) {
|
2022-10-12 22:53:16 +03:00
|
|
|
stopwatch.zero();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
2023-10-26 08:29:45 +03:00
|
|
|
|
|
|
|
app.use('/control', control);
|