@@ -360,32 +360,84 @@ export default (fastify, options, done) => {
360
360
const hostname = request . hostname ; // 主机名,不包含端口
361
361
const port = request . socket . localPort ; // 获取当前服务的端口
362
362
console . log ( `cfg_path:${ cfg_path } ,port:${ port } ` ) ;
363
- let requestHost = ( cfg_path . startsWith ( '/1' ) || cfg_path . startsWith ( '/index' ) ) ? `${ protocol } ://${ hostname } ` : `http://127.0.0.1:${ options . PORT } ` ; // 动态生成根地址
364
- let requestUrl = ( cfg_path . startsWith ( '/1' ) || cfg_path . startsWith ( '/index' ) ) ? `${ protocol } ://${ hostname } ${ request . url } ` : `http://127.0.0.1:${ options . PORT } ${ request . url } ` ; // 动态生成请求链接
363
+ let not_local = cfg_path . startsWith ( '/1' ) || cfg_path . startsWith ( '/index' ) ;
364
+ let requestHost = not_local ? `${ protocol } ://${ hostname } ` : `http://127.0.0.1:${ options . PORT } ` ; // 动态生成根地址
365
+ let requestUrl = not_local ? `${ protocol } ://${ hostname } ${ request . url } ` : `http://127.0.0.1:${ options . PORT } ${ request . url } ` ; // 动态生成请求链接
365
366
// console.log('requestUrl:', requestUrl);
366
- if ( cfg_path . endsWith ( '.js' ) ) {
367
- if ( cfg_path . includes ( 'index.js' ) ) {
368
- return reply . sendFile ( 'index.js' , path . join ( options . rootDir , 'data/cat' ) ) ;
369
- } else if ( cfg_path . includes ( 'index.config.js' ) ) {
370
- let content = readFileSync ( path . join ( options . rootDir , 'data/cat/index.config.js' ) , 'utf-8' ) ;
371
- // console.log(content);
372
- content = jinja . render ( content , { config_url : requestUrl . replace ( cfg_path , '/1' ) } ) ;
373
- return reply . type ( 'application/javascript;charset=utf-8' ) . send ( content ) ;
367
+ // if (cfg_path.endsWith('.js')) {
368
+ // if (cfg_path.includes('index.js')) {
369
+ // // return reply.sendFile('index.js', path.join(options.rootDir, 'data/cat'));
370
+ // let content = readFileSync(path.join(options.rootDir, 'data/cat/index.js'), 'utf-8');
371
+ // // content = jinja.render(content, {config_url: requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`)});
372
+ // content = content.replace('$config_url', requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`));
373
+ // return reply.type('application/javascript;charset=utf-8').send(content);
374
+ // } else if (cfg_path.includes('index.config.js')) {
375
+ // let content = readFileSync(path.join(options.rootDir, 'data/cat/index.config.js'), 'utf-8');
376
+ // // content = jinja.render(content, {config_url: requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`)});
377
+ // content = content.replace('$config_url', requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`));
378
+ // return reply.type('application/javascript;charset=utf-8').send(content);
379
+ // }
380
+ // }
381
+ // if (cfg_path.endsWith('.js.md5')) {
382
+ // if (cfg_path.includes('index.js')) {
383
+ // let content = readFileSync(path.join(options.rootDir, 'data/cat/index.js'), 'utf-8');
384
+ // // content = jinja.render(content, {config_url: requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`)});
385
+ // content = content.replace('$config_url', requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`));
386
+ // let contentHash = md5(content);
387
+ // console.log('index.js contentHash:', contentHash);
388
+ // return reply.type('text/plain;charset=utf-8').send(contentHash);
389
+ // } else if (cfg_path.includes('index.config.js')) {
390
+ // let content = readFileSync(path.join(options.rootDir, 'data/cat/index.config.js'), 'utf-8');
391
+ // // content = jinja.render(content, {config_url: requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`)});
392
+ // content = content.replace('$config_url', requestUrl.replace(cfg_path, `/1?sub=all&pwd=${process.env.API_PWD || ''}`));
393
+ // let contentHash = md5(content);
394
+ // console.log('index.config.js contentHash:', contentHash);
395
+ // return reply.type('text/plain;charset=utf-8').send(contentHash);
396
+ // }
397
+ // }
398
+ const getFilePath = ( cfgPath , rootDir , fileName ) => path . join ( rootDir , `data/cat/${ fileName } ` ) ;
399
+ const processContent = ( content , cfgPath , requestUrl ) =>
400
+ content . replace ( '$config_url' , requestUrl . replace ( cfgPath , `/1?sub=all&pwd=${ process . env . API_PWD || '' } ` ) ) ;
401
+
402
+ const handleJavaScript = ( cfgPath , requestUrl , options , reply ) => {
403
+ const fileMap = {
404
+ 'index.js' : 'index.js' ,
405
+ 'index.config.js' : 'index.config.js'
406
+ } ;
407
+
408
+ for ( const [ key , fileName ] of Object . entries ( fileMap ) ) {
409
+ if ( cfgPath . includes ( key ) ) {
410
+ const filePath = getFilePath ( cfgPath , options . rootDir , fileName ) ;
411
+ let content = readFileSync ( filePath , 'utf-8' ) ;
412
+ content = processContent ( content , cfgPath , requestUrl ) ;
413
+ return reply . type ( 'application/javascript;charset=utf-8' ) . send ( content ) ;
414
+ }
374
415
}
416
+ } ;
417
+
418
+ const handleJsMd5 = ( cfgPath , requestUrl , options , reply ) => {
419
+ const fileMap = {
420
+ 'index.js' : 'index.js' ,
421
+ 'index.config.js' : 'index.config.js'
422
+ } ;
423
+
424
+ for ( const [ key , fileName ] of Object . entries ( fileMap ) ) {
425
+ if ( cfgPath . includes ( key ) ) {
426
+ const filePath = getFilePath ( cfgPath , options . rootDir , fileName ) ;
427
+ let content = readFileSync ( filePath , 'utf-8' ) ;
428
+ content = processContent ( content , cfgPath , requestUrl ) ;
429
+ const contentHash = md5 ( content ) ;
430
+ console . log ( `${ fileName } contentHash:` , contentHash ) ;
431
+ return reply . type ( 'text/plain;charset=utf-8' ) . send ( contentHash ) ;
432
+ }
433
+ }
434
+ } ;
435
+ if ( cfg_path . endsWith ( '.js' ) ) {
436
+ return handleJavaScript ( cfg_path , requestUrl , options , reply ) ;
375
437
}
438
+
376
439
if ( cfg_path . endsWith ( '.js.md5' ) ) {
377
- if ( cfg_path . includes ( 'index.js' ) ) {
378
- let content = readFileSync ( path . join ( options . rootDir , 'data/cat/index.js.md5' ) , 'utf-8' ) ;
379
- return reply . type ( 'text/plain;charset=utf-8' ) . send ( content ) ;
380
- // return reply.sendFile('index.js.md5', path.join(options.rootDir, 'data/cat'));
381
- } else if ( cfg_path . includes ( 'index.config.js' ) ) {
382
- let content = readFileSync ( path . join ( options . rootDir , 'data/cat/index.config.js' ) , 'utf-8' ) ;
383
- // console.log(content);
384
- content = jinja . render ( content , { config_url : requestUrl . replace ( cfg_path , '/1' ) } ) ;
385
- let contentHash = md5 ( content ) ;
386
- console . log ( 'contentHash:' , contentHash ) ;
387
- return reply . type ( 'text/plain;charset=utf-8' ) . send ( contentHash ) ;
388
- }
440
+ return handleJsMd5 ( cfg_path , requestUrl , options , reply ) ;
389
441
}
390
442
let sub = null ;
391
443
if ( sub_code ) {
0 commit comments