diff --git a/src/server.js b/src/server.js index 3436139..5fac449 100644 --- a/src/server.js +++ b/src/server.js @@ -16,11 +16,11 @@ const defaultConfig = { limit: 1024 * 1000, }, devMode: false, + endpoint: '/batch', files: [], logger: {}, plugins: [], port: 8080, - endpoint: '/batch', }; export default function hypernova(userConfig, onServer) { diff --git a/src/worker.js b/src/worker.js index b541e1a..35ed296 100644 --- a/src/worker.js +++ b/src/worker.js @@ -7,20 +7,17 @@ import BatchManager from './utils/BatchManager'; let closing = false; -export default (app, config, onServer, workerId) => { - let server; - - // ===== Middleware ========================================================= +const attachMiddleware = (app, config) => { app.use(bodyParser.json(config.bodyParser)); +}; - if (onServer) { - onServer(app, process); - } +const attachEndpoint = (app, config, callback) => { + app.post(config.endpoint, renderBatch(config, callback)); +}; - // ===== Routes ============================================================= - app.post(config.endpoint, renderBatch(config, () => closing)); +const initServer = (app, config, callback) => { + let server; - // ===== Exceptions ========================================================= function exit(code) { return () => process.exit(code); } @@ -93,14 +90,35 @@ export default (app, config, onServer, workerId) => { // run through the initialize methods of any plugins that define them runAppLifecycle('initialize', config.plugins, config) .then(() => { - server = app.listen(config.port, () => { - if (process.send) { - // tell our coordinator that we're ready to start receiving requests - process.send({ workerId, ready: true }); - } - - logger.info('Connected', { port: config.port }); - }); + server = app.listen(config.port, callback); }) .catch(shutDownSequence); }; + +const worker = (app, config, onServer, workerId) => { + // ===== Middleware ========================================================= + attachMiddleware(app, config); + + if (onServer) { + onServer(app, process); + } + + // ===== Routes ============================================================= + attachEndpoint(app, config, () => closing); + + // ===== initialize server's nuts and bolts ================================= + initServer(app, config, () => { + if (process.send) { + // tell our coordinator that we're ready to start receiving requests + process.send({ workerId, ready: true }); + } + + logger.info('Connected', { port: config.port }); + }); +}; + +worker.attachMiddleware = attachMiddleware; +worker.attachEndpoint = attachEndpoint; +worker.initServer = initServer; + +export default worker;