webServer.js 4.7 KB

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