-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (57 loc) · 1.59 KB
/
server.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
'use strict';
const express = require('express');
const fs = require('fs');
const http = require('http');
const path = require('path');
const herokuHost = 'erlkonig-lisp-console.herokuapp.com';
const isProduction = !!(process.env.NODE_ENV === 'production');
const host = isProduction ? herokuHost : 'localhost';
const oneHour = 60 * 60 * 1000;
const httpConfig = { host: host };
const port = process.env.PORT || 5000;
const utf8 = { encoding: 'utf8' };
const app = express();
const noncePlaceholder = /\$NONCE\$/g;
const template = fs.readFileSync('./templates/app', utf8);
if (!isProduction) {
httpConfig.port = port;
}
function exitProcess() {
process.exit();
}
function onStart() {
console.log('Server running on port ' + port + '.');
preventHerokuSleep();
}
function pingHeroku() {
http.get(httpConfig);
}
function preventHerokuSleep() {
setInterval(pingHeroku, oneHour);
console.log('Pinging app to prevent spin-down.');
}
function _uuid(i, j) {
let nbr;
if (i === 12) {
nbr = 4;
} else if (i == 16) {
nbr = j & 3 | 8;
} else {
nbr = j;
}
return nbr.toString(16);
}
function uuid() {
let id = '';
for (let i = 0; i < 32; i++) {
const random = Math.random() * 16 | 0;
id += _uuid(i, random);
}
return id;
}
const webpage = template.replace(noncePlaceholder, uuid());
app.use(express.static(path.join(__dirname, 'public'), { index: webpage }));
app.get('/', function (req, res) { res.send(webpage); });
process.on('SIGINT', exitProcess);
process.on('SIGTERM', exitProcess);
app.listen(port, onStart);