@@ -7,20 +7,17 @@ import BatchManager from './utils/BatchManager';
77
88let closing = false ;
99
10- export default ( app , config , onServer , workerId ) => {
11- let server ;
12-
13- // ===== Middleware =========================================================
10+ const attachMiddleware = ( app , config ) => {
1411 app . use ( bodyParser . json ( config . bodyParser ) ) ;
12+ } ;
1513
16- if ( onServer ) {
17- onServer ( app , process ) ;
18- }
14+ const attachEndpoint = ( app , config , callback ) => {
15+ app . post ( config . endpoint , renderBatch ( config , callback ) ) ;
16+ } ;
1917
20- // ===== Routes =============================================================
21- app . post ( '/batch' , renderBatch ( config , ( ) => closing ) ) ;
18+ const initServer = ( app , config , callback ) => {
19+ let server ;
2220
23- // ===== Exceptions =========================================================
2421 function exit ( code ) {
2522 return ( ) => process . exit ( code ) ;
2623 }
@@ -93,14 +90,35 @@ export default (app, config, onServer, workerId) => {
9390 // run through the initialize methods of any plugins that define them
9491 runAppLifecycle ( 'initialize' , config . plugins , config )
9592 . then ( ( ) => {
96- server = app . listen ( config . port , ( ) => {
97- if ( process . send ) {
98- // tell our coordinator that we're ready to start receiving requests
99- process . send ( { workerId, ready : true } ) ;
100- }
101-
102- logger . info ( 'Connected' , { port : config . port } ) ;
103- } ) ;
93+ server = app . listen ( config . port , callback ) ;
10494 } )
10595 . catch ( shutDownSequence ) ;
10696} ;
97+
98+ const worker = ( app , config , onServer , workerId ) => {
99+ // ===== Middleware =========================================================
100+ attachMiddleware ( app , config ) ;
101+
102+ if ( onServer ) {
103+ onServer ( app , process ) ;
104+ }
105+
106+ // ===== Routes =============================================================
107+ attachEndpoint ( app , config , ( ) => closing ) ;
108+
109+ // ===== initialize server's nuts and bolts =================================
110+ initServer ( app , config , ( ) => {
111+ if ( process . send ) {
112+ // tell our coordinator that we're ready to start receiving requests
113+ process . send ( { workerId, ready : true } ) ;
114+ }
115+
116+ logger . info ( 'Connected' , { port : config . port } ) ;
117+ } ) ;
118+ } ;
119+
120+ worker . attachMiddleware = attachMiddleware ;
121+ worker . attachEndpoint = attachEndpoint ;
122+ worker . initServer = initServer ;
123+
124+ export default worker ;
0 commit comments