node_vwf.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/nodejs/reflector' ),
  8. vwf = require( './lib/nodejs/vwf' ),
  9. argv = require('optimist').argv;
  10. // Basic logging function.
  11. global.log = function () {
  12. var args = Array.prototype.slice.call( arguments );
  13. var level = args.splice( args.length - 1 )[ 0 ];
  14. if ( !isNaN( parseInt( level ) ) ) {
  15. level = parseInt( level );
  16. } else {
  17. args.push( level )
  18. level = 1;
  19. };
  20. if ( level <= global.logLevel ) {
  21. console.log.apply( this, args );
  22. }
  23. };
  24. function consoleNotice( string ) {
  25. var brown = '\u001b[33m';
  26. var reset = '\u001b[0m';
  27. global.log( brown + string + reset );
  28. }
  29. function consoleError( string ) {
  30. var red = '\u001b[31m';
  31. var reset = '\u001b[0m';
  32. global.log( red + string + reset );
  33. }
  34. // Set the root directory where applications will be served from. Default
  35. // to the current directory if none is specified.
  36. // Use --applicationPath or -a to specify an alternative path.
  37. function parseApplicationPath () {
  38. if ( argv.applicationPath || argv.a ) {
  39. var applicationPath = argv.applicationPath || argv.a;
  40. if ( fs.existsSync( applicationPath ) && fs.statSync( applicationPath ).isDirectory() ) {
  41. consoleNotice( "Serving VWF applications from " + applicationPath );
  42. return applicationPath;
  43. } else {
  44. consoleError( applicationPath + " is NOT a directory! Serving VWF applications from " + process.cwd() );
  45. return process.cwd();
  46. }
  47. } else {
  48. consoleNotice( "Serving VWF applications from " + process.cwd() );
  49. return process.cwd();
  50. }
  51. }
  52. // Set the VWF directory where VWF files will be served from. Default to
  53. // user specified directory if defined by the command line "-v" or "--vwfPath"
  54. // options, then current working directory, and finally if not found at either,
  55. // try the "$HOME/.vwf" directory.
  56. function parseVWFPath () {
  57. var home = ( process.env.HOME || process.env.USERPROFILE );
  58. var vwfHome = path.join( home, ".vwf" );
  59. var vwfPath = ( argv.v || argv.vwfPath );
  60. if ( vwfPath != undefined && fs.existsSync( path.join( vwfPath, "support/client/lib" ) ) ) {
  61. return vwfPath;
  62. } else if ( fs.existsSync( path.join( process.cwd(), "support/client/lib" ) ) ) {
  63. return process.cwd();
  64. } else if ( fs.existsSync( path.join( vwfHome, "support/client/lib" ) ) ) {
  65. return vwfHome;
  66. } else if ( process.env.VWF_DIR && fs.existsSync( path.join( process.env.VWF_DIR, "support/client/lib" ) ) ) {
  67. return process.env.VWF_DIR;
  68. } else {
  69. consoleError( "Could not find VWF support files." );
  70. return false;
  71. }
  72. }
  73. //Start the VWF server
  74. function startVWF() {
  75. global.logLevel = ( ( argv.l || argv.log ) ? ( argv.l || argv.log ) : 1 );
  76. global.vwfRoot = parseVWFPath();
  77. global.instances = {};
  78. if ( !global.vwfRoot ) {
  79. // Should not hit this path since the VWF script checks for the existence
  80. // of the VWF support files before running this script.
  81. consoleError("Exiting.");
  82. process.exit();
  83. }
  84. function OnRequest( request, response ) {
  85. try {
  86. vwf.Serve( request, response );
  87. } catch ( e ) {
  88. response.writeHead( 500, {
  89. "Content-Type": "text/plain"
  90. } );
  91. response.write( e.toString(), "utf8" );
  92. response.end();
  93. }
  94. } // close onRequest
  95. consoleNotice( 'LogLevel = ' + global.logLevel );
  96. consoleNotice( 'Serving VWF support files from ' + global.vwfRoot );
  97. if ( argv.nocache ) {
  98. FileCache.enabled = false;
  99. consoleNotice( 'server cache disabled' );
  100. }
  101. global.applicationRoot = parseApplicationPath( );
  102. var ssl = ( argv.s || argv.ssl );
  103. var pass = ( ( argv.w) ? ( argv.w) : undefined );
  104. var sslOptions = {
  105. key: ( ( argv.k || argv.key ) ? fs.readFileSync( argv.k || argv.key ) : undefined ),
  106. cert: ( ( argv.c || argv.cert ) ? fs.readFileSync( argv.c || argv.cert ) : undefined ),
  107. passphrase: JSON.stringify(pass)
  108. };
  109. //create the server
  110. var port = ( ( argv.p || argv.port ) ? ( argv.p || argv.port ) : 3001 );
  111. var srv = ssl ? https.createServer( sslOptions, OnRequest ).listen( port ) : http.createServer( OnRequest ).listen( port );
  112. consoleNotice( 'Serving on port ' + port );
  113. var socketManager = sio.listen(srv,
  114. {
  115. log: false
  116. } );
  117. socketManager.set('transports', ['websocket']);
  118. socketManager.sockets.on( 'connection', reflector.OnConnection );
  119. }
  120. exports.startVWF = startVWF;