diff --git a/README.md b/README.md index e1c0e56..134d05e 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,9 @@ Options, and their defaults // the port the app will start on port: 8080, // default endpoint path - endpoint: '/batch' + endpoint: '/batch', + // default close callback + getClose: () => {}, } ``` diff --git a/src/server.js b/src/server.js index e2ebe1b..ca73098 100644 --- a/src/server.js +++ b/src/server.js @@ -22,6 +22,7 @@ const defaultConfig = { plugins: [], port: 8080, host: '0.0.0.0', + getClose: () => {}, }; export default function hypernova(userConfig, onServer) { @@ -31,7 +32,10 @@ export default function hypernova(userConfig, onServer) { throw new TypeError('Hypernova requires a `getComponent` property and it must be a function'); } - logger.init(config.logger); + // don't initialize logger if it has already been initialized + if (logger.init) { + logger.init(config.logger); + } const app = express(); diff --git a/src/worker.js b/src/worker.js index c00271a..3320a60 100644 --- a/src/worker.js +++ b/src/worker.js @@ -16,9 +16,9 @@ const attachEndpoint = (app, config, callback) => { app.post(config.endpoint, renderBatch(config, callback)); }; + const initServer = (app, config, callback) => { let server; - function exit(code) { return () => process.exit(code); } @@ -54,6 +54,10 @@ const initServer = (app, config, callback) => { .catch(exit(code)); } + function readyToClose(getClose) { + getClose(shutDownSequence); + } + function errorHandler(err, req, res, next) { // eslint-disable-line no-unused-vars // If there is an error with body-parser and the status is set then we can safely swallow // the error and report it. @@ -91,7 +95,10 @@ const initServer = (app, config, callback) => { // run through the initialize methods of any plugins that define them runAppLifecycle('initialize', config.plugins, config) .then(() => { - server = app.listen(config.port, config.host, callback); + server = app.listen(config.port, config.host, () => { + callback(); + readyToClose(config.getClose); + }); return null; }) .catch(shutDownSequence); diff --git a/test/server-test.js b/test/server-test.js index 2fcc8ae..8f05f1b 100644 --- a/test/server-test.js +++ b/test/server-test.js @@ -1,13 +1,55 @@ import { assert } from 'chai'; +import sinon from 'sinon'; import hypernova from '../server'; describe('Hypernova server', () => { + let sandbox; + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + sandbox.stub(process, 'exit'); + }); + + afterEach(() => { + sandbox.restore(); + }); + it('blows up if hypernova does not get getComponent', () => { assert.throws(hypernova, TypeError); }); - it('starts up the hypernova server without blowing up', () => { - hypernova({ devMode: true, getComponent: () => {} }); + it('starts up the hypernova server without blowing up', (done) => { + hypernova({ + devMode: true, + getComponent: () => {}, + plugins: [ + { + shutDown: () => { + done(); + }, + }, + ], + getClose: (close) => { + close(null, null, 0); + }, + }); + }); + + it('starts up the hypernova server again without blowing up', (done) => { + hypernova({ + devMode: true, + getComponent: () => {}, + plugins: [ + { + shutDown: () => { + done(); + }, + }, + ], + getClose: (close) => { + close(null, null, 0); + }, + }); }); });