index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var express = require('express'),
  2. compression = require('compression'),
  3. serveStatic = require('serve-static'),
  4. serveIndex = require('serve-index'),
  5. cors = require('cors'),
  6. morgan = require('morgan'),
  7. path = require('path'),
  8. fs = require('fs'),
  9. argv = require('optimist').argv,
  10. http = require('http'),
  11. https = require('https');
  12. // var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
  13. // var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
  14. var app = express();
  15. var port = 3007;
  16. app.use(compression());
  17. app.use(serveStatic(__dirname + '/public'));
  18. app.use(cors());
  19. // app.use(function(req, res, next) {
  20. // res.header("Access-Control-Allow-Origin", "*");
  21. // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  22. // next();
  23. // });
  24. app.use(morgan('combined'));
  25. /*=====Site specific paths=====*/
  26. // optional functions to load defaults (not required if DB is already bootstrapped)
  27. function readDirR(dir) {
  28. if (fs.statSync(dir).isDirectory())
  29. {
  30. return Array.prototype.concat(...fs.readdirSync(dir).map(f => readDirR(path.join(dir, f))
  31. ))
  32. } else {
  33. if ((dir.indexOf('.yaml') !== -1) || (dir.indexOf('.js') !== -1) || (dir.indexOf('.html') !== -1)
  34. || (dir.indexOf('.json') !== -1))
  35. return dir.replace(__dirname + '/public/', "/")
  36. }
  37. }
  38. app.get('/proxy-files', function (req, res) {
  39. // console.log(allFilesSync(__dirname + '/public/proxy/'));
  40. res.writeHead(200, {"Content-Type": "application/json"});
  41. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/proxy/'));
  42. res.end(json);
  43. });
  44. app.get('/world-files', function (req, res) {
  45. // console.log(allFilesSync(__dirname + '/public/defaults/templates/'));
  46. res.writeHead(200, {"Content-Type": "application/json"});
  47. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/worlds/'));
  48. res.end(json);
  49. });
  50. // send all requests to index.html so browserHistory in React Router works
  51. app.get('*', function (req, res) {
  52. res.sendFile(path.join(__dirname + '/public/', 'index.html'))
  53. })
  54. //=========end of specific===========
  55. // app.listen(port);
  56. // console.log('Web server is started on port: '+ port);
  57. var ssl = ( argv.s || argv.ssl );
  58. var pass = ( ( argv.w) ? ( argv.w) : undefined );
  59. var sslOptions = {
  60. key: ( ( argv.k || argv.key ) ? fs.readFileSync( argv.k || argv.key ) : undefined ),
  61. cert: ( ( argv.c || argv.cert ) ? fs.readFileSync( argv.c || argv.cert ) : undefined ),
  62. ca: ( ( argv.t || argv.ca ) ? fs.readFileSync( argv.t || argv.ca ) : undefined ),
  63. passphrase: JSON.stringify(pass)
  64. };
  65. //create the server
  66. var port = ( ( argv.p || argv.port ) ? ( argv.p || argv.port ) : 3007 );
  67. var srv = ssl ? https.createServer( sslOptions, app ).listen( port ) : http.createServer( app ).listen( port );
  68. console.log( 'Serving on port ' + port );