-
Notifications
You must be signed in to change notification settings - Fork 53
/
node_vwf.js
157 lines (128 loc) · 4.99 KB
/
node_vwf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var path = require( 'path' ),
http = require( 'http' ),
https = require( 'https' ),
fs = require( 'fs' ),
url = require( 'url' ),
sio = require( 'socket.io' ),
reflector = require( './lib/nodejs/reflector' ),
vwf = require( './lib/nodejs/vwf' ),
argv = require('optimist').argv;
// Basic logging function.
global.log = function () {
var args = Array.prototype.slice.call( arguments );
var level = args.splice( args.length - 1 )[ 0 ];
if ( !isNaN( parseInt( level ) ) ) {
level = parseInt( level );
} else {
args.push( level )
level = 1;
};
if ( level <= global.logLevel ) {
console.log.apply( this, args );
}
};
function consoleNotice( string ) {
var brown = '\u001b[33m';
var reset = '\u001b[0m';
global.log( brown + string + reset );
}
function consoleError( string ) {
var red = '\u001b[31m';
var reset = '\u001b[0m';
global.log( red + string + reset );
}
// Set the root directory where applications will be served from. Default
// to the current directory if none is specified.
// Use --applicationPath or -a to specify an alternative path.
function parseApplicationPath () {
if ( argv.applicationPath || argv.a ) {
var applicationPath = argv.applicationPath || argv.a;
if ( fs.existsSync( applicationPath ) && fs.statSync( applicationPath ).isDirectory() ) {
consoleNotice( "Serving VWF applications from " + applicationPath );
return applicationPath;
} else {
consoleError( applicationPath + " is NOT a directory! Serving VWF applications from " + process.cwd() );
return process.cwd();
}
} else {
consoleNotice( "Serving VWF applications from " + process.cwd() );
return process.cwd();
}
}
// Set the VWF directory where VWF files will be served from. Default to
// user specified directory if defined by the command line "-v" or "--vwfPath"
// options, then current working directory, and finally if not found at either,
// try the "$HOME/.vwf" directory.
function parseVWFPath () {
var home = ( process.env.HOME || process.env.USERPROFILE );
var vwfHome = path.join( home, ".vwf" );
var vwfPath = ( argv.v || argv.vwfPath );
if ( vwfPath != undefined && fs.existsSync( path.join( vwfPath, "support/client/lib" ) ) ) {
return vwfPath;
} else if ( fs.existsSync( path.join( process.cwd(), "support/client/lib" ) ) ) {
return process.cwd();
} else if ( fs.existsSync( path.join( vwfHome, "support/client/lib" ) ) ) {
return vwfHome;
} else if ( process.env.VWF_DIR && fs.existsSync( path.join( process.env.VWF_DIR, "support/client/lib" ) ) ) {
return process.env.VWF_DIR;
} else {
consoleError( "Could not find VWF support files." );
return false;
}
}
//Start the VWF server
function startVWF() {
global.logLevel = ( ( argv.l || argv.log ) ? ( argv.l || argv.log ) : 1 );
global.vwfRoot = parseVWFPath();
global.instances = {};
if ( !global.vwfRoot ) {
// Should not hit this path since the VWF script checks for the existence
// of the VWF support files before running this script.
consoleError("Exiting.");
process.exit();
}
function OnRequest( request, response ) {
try {
vwf.Serve( request, response );
} catch ( e ) {
response.writeHead( 500, {
"Content-Type": "text/plain"
} );
response.write( e.toString(), "utf8" );
response.end();
}
} // close onRequest
consoleNotice( 'LogLevel = ' + global.logLevel );
consoleNotice( 'Serving VWF support files from ' + global.vwfRoot );
if ( argv.nocache ) {
FileCache.enabled = false;
consoleNotice( 'server cache disabled' );
}
global.applicationRoot = parseApplicationPath( );
var ssl = ( argv.s || argv.ssl );
var sslOptions = {
key: ( ( argv.k || argv.key ) ? fs.readFileSync( argv.k || argv.key ) : undefined ),
cert: ( ( argv.c || argv.cert ) ? fs.readFileSync( argv.c || argv.cert ) : undefined )
};
//create the server
var port = ( ( argv.p || argv.port ) ? ( argv.p || argv.port ) : 3000 );
var srv = ssl ? https.createServer( sslOptions, OnRequest ).listen( port ) : http.createServer( OnRequest ).listen( port );
consoleNotice( 'Serving on port ' + port );
//create socket server
var socketManager = sio.listen( srv, {
log: false,
resource: {
exec: function( url ) {
var match = /\/1\/\?t=\d*/.exec( url ) || /\/1\/websocket/.exec( url );
if (match) {
return [url.substring(0, url.indexOf(match[0]))];
} else {
return null;
}
}
}
} );
socketManager.set( 'transports', [ 'websocket' ] );
socketManager.sockets.on( 'connection', reflector.OnConnection );
}
exports.startVWF = startVWF;