From 3f7d0d75640bebcaa796938ef7980ae9cef5075f Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Mon, 27 Jun 2016 11:35:21 -0700 Subject: [PATCH 1/2] Made endpoint configurable. --- README.md | 2 ++ src/server.js | 1 + src/worker.js | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d0d71b..c0a72eb 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,8 @@ Options, and their defaults // the port the app will start on port: 8080, // whether or not to run in parallel using all available cpus + endpoint: '/batch' + // default endpoint path } ``` diff --git a/src/server.js b/src/server.js index 4489666..3436139 100644 --- a/src/server.js +++ b/src/server.js @@ -20,6 +20,7 @@ const defaultConfig = { logger: {}, plugins: [], port: 8080, + endpoint: '/batch', }; export default function hypernova(userConfig, onServer) { diff --git a/src/worker.js b/src/worker.js index 3bdcb40..b541e1a 100644 --- a/src/worker.js +++ b/src/worker.js @@ -18,7 +18,7 @@ export default (app, config, onServer, workerId) => { } // ===== Routes ============================================================= - app.post('/batch', renderBatch(config, () => closing)); + app.post(config.endpoint, renderBatch(config, () => closing)); // ===== Exceptions ========================================================= function exit(code) { From 864fc997394eba969a2d6de8db16249f7150526a Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Mon, 27 Jun 2016 14:06:23 -0700 Subject: [PATCH 2/2] Splits worker all-in-one function into pieces --- src/server.js | 2 +- src/worker.js | 54 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 19 deletions(-) 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;