node_vwf.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var path = require('path'),
  2. http = require('http'),
  3. https = require('https'),
  4. fs = require('fs'),
  5. url = require('url'),
  6. sio = require('socket.io'),
  7. reflector = require('./lib/reflector'),
  8. argv = require('optimist').argv;
  9. function printGeneralHelp() {
  10. console.log("Options:");
  11. console.log(" -p, --port Port to start server on. Default: 3000");
  12. console.log(" -l, --log Log level for server. Default: 1");
  13. console.log(" -h, --help Output usage information");
  14. console.log(" -s, --ssl Enables SSL");
  15. console.log(" -k, --key Path to private key");
  16. console.log(" -c, --cert Path to certificate");
  17. }
  18. // Basic logging function.
  19. global.log = function () {
  20. var args = Array.prototype.slice.call(arguments);
  21. var level = args.splice(args.length - 1)[0];
  22. if (!isNaN(parseInt(level))) {
  23. level = parseInt(level);
  24. } else {
  25. args.push(level)
  26. level = 1;
  27. };
  28. if (level <= global.logLevel) {
  29. console.log.apply(this, args);
  30. }
  31. };
  32. function consoleNotice(string) {
  33. var brown = '\u001b[33m';
  34. var reset = '\u001b[0m';
  35. global.log(brown + string + reset);
  36. }
  37. function consoleError(string) {
  38. var red = '\u001b[31m';
  39. var reset = '\u001b[0m';
  40. global.log(red + string + reset);
  41. }
  42. //Start the VWF server
  43. function startVWF() {
  44. global.logLevel = ((argv.l || argv.log) ? (argv.l || argv.log) : 1);
  45. global.instances = {};
  46. function serve(request, response){
  47. response.writeHead( 200, {
  48. "Content-Type": "application/json"
  49. } );
  50. var inst = Object.keys(global.instances);
  51. var jsonobject = {
  52. "reflector": "v0.0.1"
  53. //"instances": inst
  54. }
  55. response.write( JSON.stringify( jsonobject ), "utf8" );
  56. response.end();
  57. //console.log("Serve here")
  58. }
  59. function OnRequest(request, response) {
  60. try {
  61. serve(request, response);
  62. // vwf.Serve( request, response );
  63. } catch (e) {
  64. response.writeHead(500, {
  65. "Content-Type": "text/plain"
  66. });
  67. response.write(e.toString(), "utf8");
  68. response.end();
  69. }
  70. } // close onRequest
  71. consoleNotice('LogLevel = ' + global.logLevel);
  72. //consoleNotice( 'Serving VWF support files from ' + global.vwfRoot );
  73. if (argv.nocache) {
  74. FileCache.enabled = false;
  75. consoleNotice('server cache disabled');
  76. }
  77. var ssl = (argv.s || argv.ssl);
  78. var pass = ((argv.w) ? (argv.w) : undefined);
  79. var sslOptions = {
  80. key: ((argv.k || argv.key) ? fs.readFileSync(argv.k || argv.key) : undefined),
  81. cert: ((argv.c || argv.cert) ? fs.readFileSync(argv.c || argv.cert) : undefined),
  82. passphrase: JSON.stringify(pass)
  83. };
  84. //create the server
  85. var port = ((argv.p || argv.port) ? (argv.p || argv.port) : 3001);
  86. var srv = ssl ? https.createServer(sslOptions, OnRequest).listen(port) : http.createServer(OnRequest).listen(port);
  87. consoleNotice('Serving on port ' + port);
  88. var socketManager = sio.listen(srv, { log: false });
  89. socketManager.set('transports', ['websocket']);
  90. socketManager.sockets.on('connection', reflector.OnConnection);
  91. }
  92. exports.startVWF = startVWF;