1-
21import express from "express" ;
32import bodyParser from "body-parser" ;
43import url from "url" ;
54import { msleep } from "./sleep.js" ;
65import processCall from "./processCall.js" ;
76
8-
97function createApp ( config , logger , customCodeDir ) {
8+ const endpoints = config . endpoints . http ;
109
11- const endpoints = config . endpoints . http ;
12-
13-
14- Object . keys ( endpoints ) . forEach ( function ( key ) {
15- if ( ! key . startsWith ( "/" ) ) {
16- endpoints [ "/" + key ] = endpoints [ key ] ;
17- delete endpoints [ key ] ;
18- }
19- } ) ;
10+ Object . keys ( endpoints ) . forEach ( function ( key ) {
11+ if ( ! key . startsWith ( "/" ) ) {
12+ endpoints [ "/" + key ] = endpoints [ key ] ;
13+ delete endpoints [ key ] ;
14+ }
15+ } ) ;
2016
21- const app = express ( ) ;
17+ const app = express ( ) ;
2218
23- app . use ( bodyParser . json ( ) ) ; // for parsing application/json
24- app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; // for parsing application/x-www-form-urlencoded
19+ app . use ( bodyParser . json ( ) ) ; // for parsing application/json
20+ app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; // for parsing application/x-www-form-urlencoded
2521
26- app . use ( ( req , res , next ) => {
27- const start = process . hrtime ( ) ;
28- res . on ( 'finish' , ( ) => {
29- const duration = process . hrtime ( start ) ;
30- const responseTime = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
31- const logMessage = `${ req . ip } - "${ req . method } ${ req . originalUrl } HTTP/${ req . httpVersion } " ${ res . statusCode } ${ res . get ( 'Content-Length' ) || 0 } "${ req . headers [ 'user-agent' ] } " - ${ responseTime . toFixed ( 3 ) } ms` ;
32- logger . debug ( logMessage ) ;
33- } ) ;
34- next ( ) ;
22+ app . use ( ( req , res , next ) => {
23+ const start = process . hrtime ( ) ;
24+ res . on ( "finish" , ( ) => {
25+ const duration = process . hrtime ( start ) ;
26+ const responseTime = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
27+ const logMessage = `${ req . ip } - "${ req . method } ${ req . originalUrl } HTTP/${ req . httpVersion } " ${ res . statusCode } ${ res . get ( "Content-Length" ) || 0 } "${ req . headers [ "user-agent" ] } " - ${ responseTime . toFixed ( 3 ) } ms` ;
28+ logger . debug ( logMessage ) ;
3529 } ) ;
36-
37- async function processRequest ( req , res , params ) {
38- const path = new URL ( req . url , `http://${ req . headers . host } ` ) . pathname ;
39- logger . info ( "Request Headers:" , req . headers ) ;
40- if ( endpoints . hasOwnProperty ( path ) ) {
41- try {
42- const results = [ ] ;
43- for ( let i = 0 ; i < endpoints [ path ] . length ; i ++ ) {
44- const call = endpoints [ path ] [ i ] ;
45- results . push ( await processCall ( call , req , logger , customCodeDir ) ) ;
46- }
47- if ( req . query . output && req . query . output === "javascript" ) {
48- res . send ( results ) ;
49- } else {
50- res . send ( results ) ;
51- }
52- } catch ( reason ) {
53- logger . error ( reason ) ;
54- res
55- . status ( typeof reason . code === "number" ? reason . code : 500 )
56- . send ( reason . message ) ;
57- }
30+ next ( ) ;
31+ } ) ;
32+
33+ async function processRequest ( req , res , params ) {
34+ const path = new URL ( req . url , `http://${ req . headers . host } ` ) . pathname ;
35+ logger . info ( "Request Headers:" , req . headers ) ;
36+ if ( endpoints . hasOwnProperty ( path ) ) {
37+ try {
38+ const results = [ ] ;
39+ for ( let i = 0 ; i < endpoints [ path ] . length ; i ++ ) {
40+ const call = endpoints [ path ] [ i ] ;
41+ results . push ( await processCall ( call , req , logger , customCodeDir ) ) ;
42+ }
43+ if ( req . query . output && req . query . output === "javascript" ) {
44+ res . send ( results ) ;
5845 } else {
59- res . status ( 404 ) . send ( "404" ) ;
46+ res . send ( results ) ;
6047 }
48+ } catch ( reason ) {
49+ logger . error ( reason ) ;
50+ res
51+ . status ( typeof reason . code === "number" ? reason . code : 500 )
52+ . send ( reason . message ) ;
53+ }
54+ } else {
55+ res . status ( 404 ) . send ( "404" ) ;
6156 }
57+ }
6258
59+ app . get ( "/**" , function ( req , res ) {
60+ processRequest ( req , res , req . query ) ;
61+ } ) ;
6362
63+ app . post ( "/**" , function ( req , res ) {
64+ processRequest ( req , res , req . body ) ;
65+ } ) ;
6466
65-
66- app . get ( "/**" , function ( req , res ) {
67- processRequest ( req , res , req . query ) ;
68- } ) ;
69-
70- app . post ( "/**" , function ( req , res ) {
71- processRequest ( req , res , req . body ) ;
72- } ) ;
73-
74-
75- return app ;
67+ return app ;
7668}
7769
7870function startServer ( config , logger , customCodeDir , port = 8080 ) {
79-
80- const app = createApp ( config , logger , customCodeDir )
81-
82- const server = app . listen ( port , ( ) =>
83- logger . info ( `Running ${ config . name } (type: ${ config . type } ) on port ${ port } ` ) ,
84- ) ;
85- logger . debug ( `Configuration:` ) ;
86- logger . debug ( JSON . stringify ( config ) ) ;
87-
88- if ( config . hasOwnProperty ( "options" ) ) {
89- server . on ( "connection" , ( socket ) => {
90- if ( config . options . hasOwnProperty ( "connectionDelay" ) ) {
91- msleep ( config . options . connectionDelay ) ;
92- }
93- if (
94- config . options . hasOwnProperty ( "lossRate" ) &&
95- parseFloat ( config . options . lossRate ) >= Math . random ( )
96- ) {
97- socket . end ( ) ;
98- throw new Error ( "An error occurred" ) ;
99- }
100- } ) ;
101- }
71+ const app = createApp ( config , logger , customCodeDir ) ;
72+
73+ const server = app . listen ( port , ( ) =>
74+ logger . info (
75+ `Running ${ config . name } (type: ${ config . type } ) on port ${ port } ` ,
76+ ) ,
77+ ) ;
78+ logger . debug ( `Configuration:` ) ;
79+ logger . debug ( JSON . stringify ( config ) ) ;
80+
81+ if ( config . hasOwnProperty ( "options" ) ) {
82+ server . on ( "connection" , ( socket ) => {
83+ if ( config . options . hasOwnProperty ( "connectionDelay" ) ) {
84+ msleep ( config . options . connectionDelay ) ;
85+ }
86+ if (
87+ config . options . hasOwnProperty ( "lossRate" ) &&
88+ parseFloat ( config . options . lossRate ) >= Math . random ( )
89+ ) {
90+ socket . end ( ) ;
91+ throw new Error ( "An error occurred" ) ;
92+ }
93+ } ) ;
94+ }
10295}
10396
104- export { createApp , startServer } ;
97+ export { createApp , startServer } ;
0 commit comments