webServer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. var express = require('express'),
  6. compression = require('compression'),
  7. serveStatic = require('serve-static'),
  8. cors = require('cors'),
  9. morgan = require('morgan'),
  10. path = require('path'),
  11. fs = require('fs'),
  12. http = require('http'),
  13. https = require('https'),
  14. //logger = require('./server/logger'),
  15. config = require('./server/readConfig')
  16. var app = express();
  17. // var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
  18. // var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
  19. function registerGunDB(srv){
  20. if (global.configuration.db === undefined)
  21. global.configuration.db = false;
  22. if (global.configuration.db){
  23. console.log('register gun db node...\n');
  24. var Gun = require('gun')
  25. require('gun/sea')
  26. require('gun/lib/path')
  27. require('gun/lib/not')
  28. require('gun/nts')
  29. require('gun/lib/bye')
  30. app.use(Gun.serve);
  31. global.gun = Gun({ web: srv, axe: false});
  32. //GLOBAL HEARTBEAT SAMPLE
  33. setInterval(function () {
  34. let message = {
  35. parameters: [],
  36. time: 'tick'//hb
  37. };
  38. global.gun.get('server').get('heartbeat').get('tick').put(JSON.stringify(message),function(ack){
  39. if(ack.err){
  40. console.log('ERROR: ' + ack.err)
  41. }});
  42. }, 50);
  43. }
  44. }
  45. function registerReflector(srv) {
  46. if (global.configuration.reflector === undefined)
  47. global.configuration.reflector = false;
  48. if (global.configuration.reflector) {
  49. //logger.info('register reflector server...\n');
  50. console.log('register reflector server...\n');
  51. var reflectorServer = require('lcs-reflector'),
  52. sio = require('socket.io')(srv);
  53. sio.set('transports', ['websocket']);
  54. sio.sockets.on('connection', reflectorServer.OnConnection);
  55. global.instances = {};
  56. }
  57. }
  58. function startServer() {
  59. //logger.info('Welcome to LiveCoding.space App server!\n');
  60. console.log('Welcome to LiveCoding.space App server!\n');
  61. config.readConfigFile();
  62. app.use(compression());
  63. app.use(serveStatic(__dirname + '/public'));
  64. app.use(cors());
  65. // app.use(function(req, res, next) {
  66. // res.header("Access-Control-Allow-Origin", "*");
  67. // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  68. // next();
  69. // });
  70. app.use(morgan('combined'));
  71. /*=====Site specific paths=====*/
  72. // optional functions to load defaults (not required if DB is already bootstrapped)
  73. function readDirR(dir) {
  74. if (fs.statSync(dir).isDirectory()) {
  75. return Array.prototype.concat(...fs.readdirSync(dir).map(f => readDirR(path.join(dir, f))
  76. ))
  77. } else {
  78. if ((dir.indexOf('.yaml') !== -1) || (dir.indexOf('.js') !== -1) || (dir.indexOf('.html') !== -1)
  79. || (dir.indexOf('.json') !== -1))
  80. // a little hack to resolve serving file paths under PC/Windows file system...
  81. return dir.replace(__dirname, '').replace(/\\/g, '/').replace('/public/', "/");
  82. }
  83. }
  84. app.get('/proxy-files', function (req, res) {
  85. // console.log(allFilesSync(__dirname + '/public/proxy/'));
  86. res.writeHead(200, { "Content-Type": "application/json" });
  87. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/proxy/'));
  88. res.end(json);
  89. });
  90. app.get('/world-files', function (req, res) {
  91. // console.log(allFilesSync(__dirname + '/public/defaults/templates/'));
  92. res.writeHead(200, { "Content-Type": "application/json" });
  93. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/worlds/'));
  94. res.end(json);
  95. });
  96. // send all requests to index.html so browserHistory in React Router works
  97. app.get('*', function (req, res) {
  98. res.sendFile(path.join(__dirname + '/public/', 'index.html'))
  99. })
  100. //=========end of specific===========
  101. // app.listen(port);
  102. // console.log('Web server is started on port: '+ port);
  103. var conf = config.parseConfigOptions();
  104. var srv = conf.ssl ? https.createServer(conf.sslOptions, app).listen(conf.port) : http.createServer(app).listen(conf.port);
  105. console.log('Serving on port ' + conf.port);
  106. registerReflector(srv);
  107. registerGunDB(srv);
  108. }
  109. exports.start = startServer;