index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // a little hack to resolve serving file paths under PC/Windows file system...
  36. return dir.replace(__dirname, '').replace(/\\/g, '/').replace('/public/',"/");
  37. }
  38. }
  39. app.get('/proxy-files', function (req, res) {
  40. // console.log(allFilesSync(__dirname + '/public/proxy/'));
  41. res.writeHead(200, {"Content-Type": "application/json"});
  42. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/proxy/'));
  43. res.end(json);
  44. });
  45. app.get('/world-files', function (req, res) {
  46. // console.log(allFilesSync(__dirname + '/public/defaults/templates/'));
  47. res.writeHead(200, {"Content-Type": "application/json"});
  48. let json = JSON.stringify(readDirR(__dirname + '/public/defaults/worlds/'));
  49. res.end(json);
  50. });
  51. // send all requests to index.html so browserHistory in React Router works
  52. app.get('*', function (req, res) {
  53. res.sendFile(path.join(__dirname + '/public/', 'index.html'))
  54. })
  55. //=========end of specific===========
  56. // app.listen(port);
  57. // console.log('Web server is started on port: '+ port);
  58. var ssl = ( argv.s || argv.ssl );
  59. var pass = ( ( argv.w) ? ( argv.w) : undefined );
  60. var sslOptions = {
  61. key: ( ( argv.k || argv.key ) ? fs.readFileSync( argv.k || argv.key ) : undefined ),
  62. cert: ( ( argv.c || argv.cert ) ? fs.readFileSync( argv.c || argv.cert ) : undefined ),
  63. ca: ( ( argv.t || argv.ca ) ? fs.readFileSync( argv.t || argv.ca ) : undefined ),
  64. passphrase: JSON.stringify(pass)
  65. };
  66. //create the server
  67. var port = ( ( argv.p || argv.port ) ? ( argv.p || argv.port ) : 3007 );
  68. var srv = ssl ? https.createServer( sslOptions, app ).listen( port ) : http.createServer( app ).listen( port );
  69. console.log( 'Serving on port ' + port );