-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
webServer.js
156 lines (111 loc) · 4.69 KB
/
webServer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
The MIT License (MIT)
Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
*/
var express = require('express'),
compression = require('compression'),
serveStatic = require('serve-static'),
cors = require('cors'),
morgan = require('morgan'),
path = require('path'),
fs = require('fs'),
http = require('http'),
https = require('https'),
//logger = require('./server/logger'),
config = require('./server/readConfig'),
history = require('connect-history-api-fallback')
var app = express();
// var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
// var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
function registerGunDB(srv){
if (global.configuration.db === undefined)
global.configuration.db = false;
if (global.configuration.db){
console.log('register gun db node...\n');
var Gun = require('gun')
require('gun/sea')
require('gun/lib/path')
require('gun/lib/not')
require('gun/nts')
require('gun/lib/bye')
app.use(Gun.serve);
global.gun = Gun({ web: srv, axe: false});
//GLOBAL HEARTBEAT SAMPLE
setInterval(function () {
let message = {
parameters: [],
time: 'tick'//hb
};
global.gun.get('server').get('heartbeat').get('tick').put(JSON.stringify(message),function(ack){
if(ack.err){
console.log('ERROR: ' + ack.err)
}});
}, 50);
}
}
function registerReflector(srv) {
if (global.configuration.reflector === undefined)
global.configuration.reflector = false;
if (global.configuration.reflector) {
//logger.info('register reflector server...\n');
console.log('register reflector server...\n');
var reflectorServer = require('lcs-reflector'),
sio = require('socket.io')(srv);
sio.set('transports', ['websocket']);
sio.sockets.on('connection', reflectorServer.OnConnection);
global.instances = {};
}
}
function startServer() {
//logger.info('Welcome to LiveCoding.space App server!\n');
console.log('Welcome to LiveCoding.space App server!\n');
config.readConfigFile();
app.use(compression());
app.use(history());
app.use(serveStatic(path.join(__dirname) + '/public'));
app.use(cors());
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
// next();
// });
app.use(morgan('combined'));
/*=====Site specific paths=====*/
// optional functions to load defaults (not required if DB is already bootstrapped)
function readDirR(dir) {
if (fs.statSync(dir).isDirectory()) {
return Array.prototype.concat(...fs.readdirSync(dir).map(f => readDirR(path.join(dir, f))
))
} else {
if ((dir.indexOf('.yaml') !== -1) || (dir.indexOf('.js') !== -1) || (dir.indexOf('.html') !== -1)
|| (dir.indexOf('.json') !== -1))
// a little hack to resolve serving file paths under PC/Windows file system...
return dir.replace(__dirname, '').replace(/\\/g, '/').replace('/public/', "/");
}
}
app.get('/proxy-files', function (req, res) {
// console.log(allFilesSync(__dirname + '/public/proxy/'));
res.writeHead(200, { "Content-Type": "application/json" });
let json = JSON.stringify(readDirR(__dirname + '/public/defaults/proxy/'));
res.end(json);
});
app.get('/world-files', function (req, res) {
// console.log(allFilesSync(__dirname + '/public/defaults/templates/'));
res.writeHead(200, { "Content-Type": "application/json" });
let json = JSON.stringify(readDirR(__dirname + '/public/defaults/worlds/'));
res.end(json);
});
// send all requests to index.html so browserHistory in React Router works
// app.get('*', function (req, res) {
// res.sendFile(path.join(__dirname + '/public/', 'index.html'))
// })
//=========end of specific===========
// app.listen(port);
// console.log('Web server is started on port: '+ port);
var conf = config.parseConfigOptions();
var srv = conf.ssl ? https.createServer(conf.sslOptions, app).listen(conf.port) : http.createServer(app).listen(conf.port);
console.log('Serving on port ' + conf.port);
registerReflector(srv);
registerGunDB(srv);
}
exports.start = startServer;