@@ -29,9 +29,10 @@ var argv = require('yargs')
29
29
. argv ;
30
30
31
31
if ( argv . help || argv . h ) {
32
- console . log ( "Usage: node app.js --port=number --debug?, where ?= {all,app,util,stream,plugin,template,scheduler,lineformatter}. " ) ;
32
+ console . log ( "Usage: node app.js [ --port=number --debug{all,app,util,stream,plugin,template,scheduler,lineformatter}=true.] " ) ;
33
33
return ;
34
34
}
35
+
35
36
if ( argv . debugall ) {
36
37
argv . debugapp = true ;
37
38
argv . debugutil = true ;
@@ -46,6 +47,7 @@ var util = require('./util.js');
46
47
var scheduler = require ( "./scheduler.js" ) ;
47
48
var stream = require ( "./stream.js" ) ;
48
49
var logger = require ( "./logger.js" ) ;
50
+ var log = require ( "../tsds2/tsdsfe/log.js" ) ;
49
51
50
52
//console.log(argv.help());
51
53
//require('v8-profiler');
@@ -133,10 +135,6 @@ app.get('/', function (req, res) {
133
135
function ( err , data ) { res . send ( data ) ; } ) ;
134
136
} )
135
137
136
- //app.get('/log', function (req, res) {
137
- // res.send(fs.readFileSync(__dirname+"/application.log", "utf8"));
138
- //})
139
-
140
138
app . get ( "/report" , function ( req , res ) {
141
139
fs . readFile ( __dirname + "/report.htm" , "utf8" ,
142
140
function ( err , data ) {
@@ -173,7 +171,6 @@ app.post("/async", function (req, res) {
173
171
} )
174
172
175
173
app . get ( "/sync" , function ( req , res ) {
176
- if ( argv . debugapp ) console . log ( "GET" )
177
174
req . setTimeout ( 1000 * 60 * 15 ) ;
178
175
handleRequest ( req , res ) ;
179
176
} ) ;
@@ -259,20 +256,27 @@ app.use("/cache", express.directory(__dirname+"/cache"));
259
256
app . use ( "/cache" , express . static ( __dirname + "/cache" ) ) ;
260
257
app . use ( "/demo" , express . directory ( __dirname + "/demo" ) ) ;
261
258
app . use ( "/demo" , express . static ( __dirname + "/demo" ) ) ;
262
- app . use ( "/test/data" , express . static ( __dirname + "/test/data" ) ) ;
263
259
app . use ( "/test/data" , express . directory ( __dirname + "/test/data" ) ) ;
260
+ app . use ( "/test/data" , express . static ( __dirname + "/test/data" ) ) ;
264
261
app . use ( "/asset" , express . directory ( __dirname + "/asset" ) ) ;
265
262
app . use ( "/asset" , express . static ( __dirname + "/asset" ) ) ;
266
263
267
- server . listen ( argv . port ) ; // Start the server
268
-
269
- util . logc ( ( new Date ( ) ) . toISOString ( ) + " - [datacache] listening on port " + argv . port , 10 ) ;
270
264
config = { } ;
271
265
config . TIMEOUT = 60 * 1000 * 15 ;
266
+ config . LOGDIR = __dirname + "/log/" ;
267
+ config . LOGHEADER = 'x-datacache-log' ;
268
+
269
+ server . listen ( argv . port ) ; // Start the server
270
+
271
+ // Create directories if needed.
272
+ config = log . init ( config )
273
+
274
+ log . logc ( ( new Date ( ) ) . toISOString ( ) + " - [datacache] listening on port " + argv . port , 10 )
275
+
272
276
server . setTimeout ( config . TIMEOUT ,
273
277
function ( obj ) {
274
- console . log ( "DataCache server timeout (" + ( config . TIMEOUT / ( 1000 * 60 ) ) + " minutes)." ) ;
275
- if ( obj ) console . log ( obj . server . _events . request ) ;
278
+ // console.log("DataCache server timeout ("+(config.TIMEOUT/(1000*60))+" minutes).");
279
+ // if (obj) console.log(obj);
276
280
} ) ;
277
281
278
282
@@ -323,31 +327,57 @@ function syncsummary(source,options,res) {
323
327
res . contentType ( 'application/json' ) ;
324
328
res . send ( JSON . stringify ( results ) ) ;
325
329
} else {
326
- console . log ( "Unknown option" ) ;
330
+ console . log ( "Unknown option for return. " ) ;
327
331
res . send ( "" ) ;
328
332
}
329
333
} ) ;
330
334
}
331
335
332
336
function handleRequest ( req , res ) {
333
337
334
- var options = parseOptions ( req ) ;
335
- var source = parseSource ( req ) ;
336
- options . id = Math . random ( ) . toString ( ) . substring ( 1 ) ;
337
- var logcolor = Math . round ( 255 * parseFloat ( options . id ) ) ;
338
+ var message = req . connection . remoteAddress + "," + req . originalUrl ;
339
+ if ( req . headers [ 'X-Forwarded-For' ] ) {
340
+ var message = req . headers [ 'X-Forwarded-For' ] . replace ( "," , ";" ) + req . originalUrl + "," ;
341
+ }
342
+
343
+ // Create detailed log file name based on current time and other information.
344
+ var loginfo = crypto . createHash ( "md5" ) . update ( ( new Date ( ) ) . toISOString ( ) + message ) . digest ( "hex" ) ;
345
+
346
+ // Set log file name as response header
347
+ res . header ( config . LOGHEADER , loginfo )
348
+
349
+ res . config = config ;
350
+
351
+ log . logapp ( loginfo + "," + message , res )
352
+
353
+ var options = parseOptions ( req , res ) ;
354
+ var source = parseSource ( req , res ) ;
355
+ options . id = Math . random ( ) . toString ( ) . substring ( 1 ) ;
356
+ var logcolor = Math . round ( 255 * parseFloat ( options . id ) ) ;
357
+
358
+ if ( argv . debugapp ) {
359
+ //log.logres("Configuration file = "+JSON.stringify(config.CONFIGFILE), res)
360
+ log . logres ( "Configuration = " + JSON . stringify ( config ) , res )
361
+ log . logres ( "req.headers = " + JSON . stringify ( req . headers ) , res )
362
+ log . logres ( "req.connection.remoteAddress = " + JSON . stringify ( req . connection . remoteAddress ) , res )
363
+ log . logres ( "req.originalUrl = " + JSON . stringify ( req . originalUrl ) , res )
364
+ log . logres ( "options = " + JSON . stringify ( options ) , res )
365
+ }
338
366
339
367
// Compress response if headers accept it and streamGzip is not requested.
340
368
if ( ! options . streamGzip )
341
369
app . use ( express . compress ( ) ) ;
342
370
343
- // Return nothing if no urls were requested or if template did create any urls.
371
+ // Return nothing if no URLs were requested or if template did create any urls.
344
372
if ( source . length === 0 ) {
373
+ log . logres ( "source.length = 0. Sending res.end()." , res )
345
374
res . end ( ) ;
346
375
return ;
347
376
}
348
377
349
- if ( options . debugapp || options . debugstream )
350
- util . logc ( options . id + " app.handleRequest() called with source=" + source . toString ( ) . replace ( / , / g, "\n\t" ) , logcolor ) ;
378
+ if ( options . debugapp || options . debugstream ) {
379
+ util . logc ( options . id + " app.handleRequest() called with source=" + source . toString ( ) . replace ( / , / g, "\n\t" ) , logcolor )
380
+ }
351
381
352
382
if ( options . return === "stream" ) {
353
383
stream . stream ( source , options , res ) ;
@@ -356,7 +386,7 @@ function handleRequest(req, res) {
356
386
}
357
387
}
358
388
359
- function parseOptions ( req ) {
389
+ function parseOptions ( req , res ) {
360
390
361
391
function s2b ( str ) { if ( str === "true" ) { return true } else { return false } }
362
392
function s2i ( str ) { return parseInt ( str ) }
@@ -442,29 +472,28 @@ function parseOptions(req) {
442
472
}
443
473
444
474
if ( argv . debugapp ) {
445
- console . log ( "\noptions after parseOptions:" ) ;
446
- console . log ( options ) ;
475
+ log . logres ( "options after parseOptions:" + JSON . stringify ( options ) , res ) ;
447
476
}
448
477
449
478
return options ;
450
479
}
451
480
452
- function parseSource ( req ) {
481
+ function parseSource ( req , res ) {
453
482
454
- options = parseOptions ( req ) ;
483
+ options = parseOptions ( req , res ) ;
455
484
456
485
var source = req . body . source || req . query . source || "" ;
457
486
var prefix = req . body . prefix || req . query . prefix ;
458
487
459
- var template = req . body . template || req . query . template ;
488
+ var template = req . body . template || req . query . template ;
460
489
var timeRange = req . body . timeRange || req . query . timeRange ;
461
490
var indexRange = req . body . indexRange || req . query . indexRange ;
462
491
463
492
if ( ! source && ! template ) return "" ;
464
493
465
494
var sourcet = [ ] ;
466
495
467
- var opts = { } ;
496
+ var opts = { } ;
468
497
469
498
if ( template ) {
470
499
opts . template = template ;
@@ -476,10 +505,14 @@ function parseSource(req) {
476
505
opts . timeRange = timeRange ;
477
506
opts . indexRange = null ;
478
507
opts . debug = options . debugtemplate ;
479
- sourcet = expandtemplate ( opts ) ;
508
+ opts . log = true ;
509
+ ret = expandtemplate ( opts ) ;
510
+ console . log ( ret . log )
511
+ sourcet = ret . files
512
+ //sourcet = expandtemplate(opts)
480
513
if ( options . debugtemplate ) {
481
- console . log ( "sourcet = " ) ;
482
- console . log ( sourcet ) ;
514
+ log . logres ( "expandtemplate.js: " + ret . log , res )
515
+ //log.logres("sourcet = "+JSON.stringify (sourcet), res)
483
516
}
484
517
485
518
}
@@ -498,8 +531,7 @@ function parseSource(req) {
498
531
}
499
532
500
533
if ( options . debugtemplate ) {
501
- console . log ( "\noptions after parseSource:" )
502
- console . log ( options ) ;
534
+ log . logres ( "options after parseSource: " + JSON . stringify ( options ) , res )
503
535
}
504
536
505
537
if ( ( sourcet . length > 0 ) && ( source . length > 0 ) ) {
@@ -513,8 +545,7 @@ function parseSource(req) {
513
545
for ( i = 0 ; i < source . length ; i ++ ) { source [ i ] = prefix + source [ i ] ; }
514
546
515
547
if ( options . debugapp ) {
516
- console . log ( "source = " ) ;
517
- console . log ( source ) ;
548
+ log . logres ( "source = " + JSON . stringify ( source ) , res )
518
549
}
519
550
520
551
return source ;
0 commit comments