This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (58 loc) · 1.98 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
/*
Ontology Network Monitor App
Owned by: Ryu Blockchain Technologies
Written by: Wyatt Mufson
Started: May 2019
*/
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const httpApp = express();
const http = require('http');
const https = require('https');
const routes = require('./routes.js');
const utils = require('./js/utils.js');
const ontology = require('./js/ontology.js');
let domain = 'monitor.ryu.games';
let credentials = {};
if (fs.existsSync(`/etc/letsencrypt/live/${domain}/privkey.pem`)) {
const privateKey = fs.readFileSync(`/etc/letsencrypt/live/${domain}/privkey.pem`, 'utf8');
const certificate = fs.readFileSync(`/etc/letsencrypt/live/${domain}/cert.pem`, 'utf8');
const ca = fs.readFileSync(`/etc/letsencrypt/live/${domain}/chain.pem`, 'utf8');
credentials = {
key: privateKey,
cert: certificate,
ca,
};
} else {
domain = 'localhost';
}
const local = domain === 'localhost';
const httpServer = http.createServer(httpApp);
const httpsServer = local ? http.createServer(app) : https.createServer(credentials, app);
const io = require('socket.io')(httpsServer); // eslint-disable-line
const redirectServerPort = local ? 8080 : 80;
const mainServerPort = local ? 80 : 443;
let connections = 0;
app.use(bodyParser.json({ limit: '300KB' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '300KB' }));
routes(app);
io.on('connection', (socket) => {
connections += 1;
utils.showConnections(connections);
socket.on('disconnect', () => {
connections -= 1;
utils.showConnections(connections);
});
});
httpApp.get('*', (req, res) => {
res.redirect(`https://${domain}${req.url}`);
});
httpServer.listen(redirectServerPort, () => {
console.log(`Starting HTTP redirect Server running on port ${redirectServerPort}`);
});
httpsServer.listen(mainServerPort, () => {
console.log(`Starting HTTPS Server running on port ${mainServerPort}`);
ontology.start(io, local);
});