webServer.js 3.5 KB

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