JedWatson/keystone — pull request #588 by snowkeeper (2014-08-29 05:30:38+00:00)
Title: Re-factored mount to allow customization

I wanted to take a shot at re-working keystone.mount() to allow a better workflow with other modules and user customization. No worries if you have better ideas; I enjoyed the process.

I split the function into several more modules:
session, externals, renderer, statics, middleware, routes, handlers, mongo

I did not prototype the modules to Keystone. That could be done if you wanted.

To mount I loop through an array of specific order to apply each module.

For each module I check for the following attached events:
beforeDefault
skipDefault
customDefault - skipDefault: true - required to override default
afterDefault

The events are accessed by adding an object through the start method. The onMount event can function as intended and be the final event after everything is mounted.
```
keystone.start({
onMount: function() {
},
duringMount: {
routes: {
beforeDefault: function() {

/* include our greeter pages
* */
greeter.add();

},
skipDefault: 'skip',
customDefault: function(keystone) {

console.log('I ran the defaults from a custom hook');

// Route requests
var app = keystone.app;
app.use(app.router);
// Headless mode means don't bind the Keystone routes
if (!keystone.get('headless')) {
keystone.routes(app);
}
// Configure application routes
if ('function' === typeof keystone.get('routes')) {
keystone.get('routes')(app);
}
}
},

}
});
```
I almost used async for the each loop as it might avoid some developer headaches down the road, but wasn't sure your take on its use.