Skip to content

Commit

Permalink
Move Composer into Pack.compose(). Closes #1653
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 18, 2014
1 parent 6c20786 commit 2fcaa6a
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 455 deletions.
72 changes: 16 additions & 56 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@
- [`pack.require(name, options, callback)`](#packrequirename-options-callback)
- [`pack.require(names, callback)`](#packrequirenames-callback)
- [`pack.register(plugin, options, callback)`](#packregisterplugin-options-callback)
- [`Hapi.Composer`](#hapicomposer)
- [`new Composer(manifest)`](#new-composermanifest)
- [`composer.compose(callback)`](#composercomposecallback)
- [`composer.start([callback])`](#composerstartcallback)
- [`composer.stop([options], [callback])`](#composerstopoptions-callback)
- [`Pack.compose(manifest, [options], callback)`](#Packcomposemanifest-options-callback)

This comment has been minimized.

Copy link
@garthk

garthk Jun 11, 2014

s/#Pack/#pack/; the TOC link doesn't work on Safari because the anchor target is case-sensitive.

- [Plugin interface](#plugin-interface)
- [`exports.register(plugin, options, next)`](#exportsregisterplugin-options-next)
- [Root methods and properties](#root-methods-and-properties)
Expand Down Expand Up @@ -2230,21 +2226,24 @@ server.pack.register(plug, { message: 'hello' }, function (err) {
});
```

## `Hapi.Composer`
### `Pack.compose(manifest, [options], callback)`

The `Composer` provides a simple way to construct a [`Pack`](#hapipack) from a single configuration object, including configuring servers
and registering plugins.
Provides a simple way to construct a [`Pack`](#hapipack) from a single configuration object, including configuring servers
and registering plugins where:

#### `new Composer(manifest)`

Creates a `Composer` object instance where:

- `manifest` - an object or array or objects where:
- `manifest` - an object with the following keys:
- `pack` - the pack `options` as described in [`new Pack()`](#packserverhost-port-options).
- `servers` - an array of server configuration objects where:
- `host`, `port`, `options` - the same as described in [`new Server()`](#new-serverhost-port-options) with the exception that the
`cache` option is not allowed and must be configured via the pack `cache` option. The `host` and `port` keys can be set to an environment variable by prefixing the variable name with `'$env.'`.
- `plugin` - an object where each key is a plugin name, and each value is the `options` object used to register that plugin.
`cache` option is not allowed and must be configured via the pack `cache` option. The `host` and `port` keys can be set to an
environment variable by prefixing the variable name with `'$env.'`.
- `plugins` - an object where each key is a plugin name, and each value is the `options` object used to register that plugin.
- `options` - optional pack configuration used as the baseline configuration for the pack (`manifest.pack` is applied to `options`).
- `callback` - the callback method, called when all packs and servers have been created and plugins registered has the signature
`function(err, pack)` where:
- `err` - an error returned from `exports.register()`. Note that incorrect usage, bad configuration, or namespace conflicts
(e.g. among routes, methods, state) will throw an error and will not return a callback.
- `pack` - the composed Pack object.

```javascript
var Hapi = require('hapi');
Expand Down Expand Up @@ -2277,48 +2276,9 @@ var manifest = {
}
};

var composer = new Hapi.Composer(manifest);
```

#### `composer.compose(callback)`

Creates the packs described in the manifest construction where:

- `callback` - the callback method, called when all packs and servers have been created and plugins registered has the signature
`function(err)` where:
- `err` - an error returned from `exports.register()`. Note that incorrect usage, bad configuration, or namespace conflicts
(e.g. among routes, methods, state) will throw an error and will not return a callback.

```javascript
composer.compose(function (err) {

if (err) {
console.log('Failed composing');
}
});
```

#### `composer.start([callback])`

Starts all the servers in all the pack composed where:

- `callback` - the callback method called when all the servers have been started.
Hapi.Pack.composer(manifest, function (err, pack) {

```javascript
composer.start(function () {

console.log('All servers started');
});
```

#### `composer.stop([options], [callback])`

Stops all the servers in all the packs and used as described in [`server.stop([options], [callback])`](#serverstopoptions-callback).

```javascript
pack.stop({ timeout: 60 * 1000 }, function () {

console.log('All servers stopped');
pack.start();
});
```

Expand Down
82 changes: 32 additions & 50 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ internals.loadExtras = function () {
console.error('Unable to require extra file: %s (%s)', extras, err.message);
process.exit(1);
}
};

return;
}

internals.getManifest = function () {

Expand All @@ -72,7 +71,7 @@ internals.getManifest = function () {
}

return manifest;
}
};


internals.loadPacks = function (manifest, callback) {
Expand All @@ -89,71 +88,54 @@ internals.loadPacks = function (manifest, callback) {
return callback(err);
}

options.pack = { requirePath: path };
options = { requirePath: path };
callback(null, options);
});
}


internals.createComposer = function (manifest, options) {

var attached = !!internals.composer; // When composer exists events are already attached.
internals.composer = new Hapi.Composer(manifest, options);

internals.composer.compose(function (err) {

Hoek.assert(!err, 'Failed loading plugins: ' + (err && err.message));
internals.composer.start(function (err) {

Hoek.assert(!err, 'Failed starting server: ' + (err && err.message));

if (!attached) {
internals.attachEvents();
}
});
});
};


internals.attachEvents = function () {
exports.start = function () {

process.once('SIGQUIT', internals.stop); // Use kill -s QUIT {pid} to kill the servers gracefully
process.on('SIGUSR2', internals.restart); // Use kill -s SIGUSR2 {pid} to restart the servers
};
internals.loadExtras();
Hapi = require('..')
var manifest = internals.getManifest();
internals.loadPacks(manifest, function (err, options) {

if (err) {
console.error(err);
process.exit(1);
}

internals.stop = function () {
Hapi.Pack.compose(manifest, options, function (err, pack) {

internals.composer.stop(function () {
Hoek.assert(!err, 'Failed loading plugins: ' + (err && err.message));

process.exit(0);
});
};
pack.start(function (err) {

Hoek.assert(!err, 'Failed starting server: ' + (err && err.message));

internals.restart = function () {
// Use kill -s QUIT {pid} to kill the servers gracefully

console.log('Stopping...');
internals.composer.stop(function () {
process.once('SIGQUIT', function () {

console.log('Starting...');
internals.start();
});
};
pack.stop(function () {

process.exit(0);
});
});

exports.start = function () {
// Use kill -s SIGUSR2 {pid} to restart the servers

internals.loadExtras();
Hapi = require('..')
var manifest = internals.getManifest();
internals.loadPacks(manifest, function (err, options) {
process.on('SIGUSR2', function () {

if (err) {
console.error(err);
process.exit(1);
}
console.log('Stopping...');
pack.stop(function () {

internals.createComposer(manifest, options);
console.log('Starting...');
internals.start();
});
});
});
});
});
};
111 changes: 0 additions & 111 deletions lib/composer.js

This file was deleted.

1 change: 1 addition & 0 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var Methods = require('./methods');

var internals = {};


exports.execute = function (request, next) {

var finalize = function (err, result) {
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports.version = require('../package.json').version;
exports.error = exports.Error = exports.boom = exports.Boom = require('boom');
exports.Server = require('./server');
exports.Pack = require('./pack');
exports.Composer = require('./composer');

exports.state = {
prepareValue: require('./state').prepareValue
Expand Down
Loading

0 comments on commit 2fcaa6a

Please sign in to comment.