webServer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 registerReflector(srv) {
  20. if (global.configuration.reflector === undefined)
  21. global.configuration.reflector = false;
  22. if (global.configuration.reflector) {
  23. logger.info('register reflector server...\n');
  24. var reflectorServer = require('lcs-reflector'),
  25. sio = require('socket.io')(srv);
  26. sio.set('transports', ['websocket']);
  27. sio.sockets.on('connection', reflectorServer.OnConnection);
  28. global.instances = {};
  29. }
  30. }
  31. function startServer() {
  32. logger.info('Welcome to LiveCoding.space App server!\n');
  33. config.readConfigFile();
  34. app.use(compression());
  35. app.use(serveStatic(__dirname + '/public'));
  36. app.use(cors());
  37. // app.use(function(req, res, next) {
  38. // res.header("Access-Control-Allow-Origin", "*");
  39. // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  40. // next();
  41. // });
  42. app.use(morgan('combined'));
  43. /*=====Site specific paths=====*/
  44. // optional functions to load defaults (not required if DB is already bootstrapped)
  45. function readDirR(dir) {
  46. if (fs.statSync(dir).isDirectory()) {
  47. return Array.prototype.concat(...fs.readdirSync(dir).map(f => readDirR(path.join(dir, f))
  48. ))
  49. } else {
  50. if ((dir.indexOf('.yaml') !== -1) || (dir.indexOf('.js') !== -1) || (dir.indexOf('.html') !== -1)
  51. || (dir.indexOf('.json') !== -1))
  52. // a little hack to resolve serving file paths under PC/Windows file system...
  53. return dir.replace(__dirname, '').replace(/\\/g, '/').replace('/public/', "/");
  54. }
  55. }
  56. app.get('/proxy-files', function (req, res) {
  57. // console.log(allFilesSync(__dirname + '/public/proxy/'));
  58. res.writeHead(200, { "Content-Type": "application/json" });
  59. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/proxy/'));
  60. res.end(json);
  61. });
  62. app.get('/world-files', function (req, res) {
  63. // console.log(allFilesSync(__dirname + '/public/defaults/templates/'));
  64. res.writeHead(200, { "Content-Type": "application/json" });
  65. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/worlds/'));
  66. res.end(json);
  67. });
  68. // send all requests to index.html so browserHistory in React Router works
  69. app.get('*', function (req, res) {
  70. res.sendFile(path.join(__dirname + '/public/', 'index.html'))
  71. })
  72. //=========end of specific===========
  73. // app.listen(port);
  74. // console.log('Web server is started on port: '+ port);
  75. var conf = config.parseConfigOptions();
  76. var srv = conf.ssl ? https.createServer(conf.sslOptions, app).listen(conf.port) : http.createServer(app).listen(conf.port);
  77. console.log('Serving on port ' + conf.port);
  78. registerReflector(srv);
  79. }
  80. exports.start = startServer;