-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (56 loc) · 1.89 KB
/
index.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
const express = require('express');
const app = express()
const fs = require('fs');
const http = require('http');
const https = require('https');
const bodyParser = require('body-parser')
const { Server } = require("socket.io");
const isProduction = process.env.NODE_ENV != 'development';
// Certificate
const privateKey = isProduction ? fs.readFileSync('/etc/letsencrypt/live/botruneterra.com.br/privkey.pem', 'utf8') : '';
const certificate = isProduction ? fs.readFileSync('/etc/letsencrypt/live/botruneterra.com.br/cert.pem', 'utf8') : '';
const ca = isProduction ? fs.readFileSync('/etc/letsencrypt/live/botruneterra.com.br/chain.pem', 'utf8') : '';
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
var sockets = {};
app.use(express.static('assets'));
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/live-card/:stream', (req, res) => {
res.sendFile(__dirname + '/live-card.html');
});
app.post('/send-card/:stream', (req, res) => {
try{
sockets[req.params.stream].emit('card', req.body);
return res.json({
status: '200'
});
} catch (e) {
return res.json({
message: 'Não deu certo'
}).status(500);
}
});
const httpServer = http.createServer(isProduction ? (req, res) => {
if (isProduction) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
return res.end();
}
} : app).listen(80, () => {
console.log('HTTP Server running on port 80');
});
const httpsServer = https.createServer(credentials, app);
if (isProduction) {
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});
}
const io = new Server(isProduction ? httpsServer : httpServer);
io.on('connection', (socket) => {
sockets[socket.handshake.query.username] = socket;
});