1
|
|
|
const express = require('express'); |
2
|
|
|
|
3
|
|
|
const request = require('./../services/request'); |
4
|
|
|
|
5
|
|
|
const router = new express.Router({}); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Homepage route, which will redirect to page with random stream id. |
9
|
|
|
* |
10
|
|
|
* @param {Object} req HTTP request. |
11
|
|
|
* @param {Object} res HTTP response. |
12
|
|
|
*/ |
13
|
|
|
router.get('/', (req, res) => { |
14
|
|
|
const streamId = Math.random().toString(36).substring(2); |
15
|
|
|
res.redirect(`/${streamId}`); |
16
|
|
|
}); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Stream page route. Renders web page for particular stream id. |
20
|
|
|
* |
21
|
|
|
* @param {Object} req HTTP request. |
22
|
|
|
* @param {Object} res HTTP response. |
23
|
|
|
*/ |
24
|
|
|
router.get('/:streamId?', (req, res) => { |
25
|
|
|
res.render('index', { |
26
|
|
|
hostName: global.APP_HOST_NAME, |
27
|
|
|
host: global.APP_HOST, |
28
|
|
|
port: global.APP_PORT_FOR_HELP_BLOCK, |
29
|
|
|
socketIoJs: global.APP_SOCKET_IO_JS, |
30
|
|
|
streamId: req.params.streamId, |
31
|
|
|
sentryDSN: global.SENTRY_DSN_FRONTEND, |
32
|
|
|
googleAnalyticsId: global.GOOGLE_ANALYTICS_ID, |
33
|
|
|
}); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Post message into page with particular stream id. |
38
|
|
|
* |
39
|
|
|
* @emits LOG.NEW |
40
|
|
|
* |
41
|
|
|
* @param {Object} req HTTP request. |
42
|
|
|
* @param {Object} res HTTP response. |
43
|
|
|
*/ |
44
|
|
|
router.post('/:streamId?', (req, res) => { |
45
|
|
|
// Capture message sender ip address as additional information. |
46
|
|
|
const ip = request.getIp(req); |
47
|
|
|
|
48
|
|
|
// Emit WebSocket event. |
49
|
|
|
if (req.headers['content-type'] === 'application/json') { |
50
|
|
|
// Socket.io rooms disabled here because rooms provide restricted behavior, |
51
|
|
|
// for example: it is impossible to open in one browser two or three different streams (rooms). |
52
|
|
|
global.socket.emit('log', { |
53
|
|
|
streamId: req.params.streamId, format: 'json', data: req.body, ip, |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// No need to provide any payload, just empty body and success status code. |
58
|
|
|
res.status(204); |
59
|
|
|
res.send(''); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
module.exports = router; |
63
|
|
|
|