Skip to content

Commit

Permalink
Added site context and documentation for request handler hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhyder committed Sep 5, 2016
1 parent a3d7c74 commit 605878b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/http/request_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ module.exports = function RequestHandlerModule(pb) {
this.siteName = this.siteObj.displayName;
//find the controller to hand off to
var route = this.getRoute(this.url.pathname);
if (route == null) {
if (route === null) {
return this.serve404();
}
this.route = route;
Expand Down Expand Up @@ -1017,6 +1017,7 @@ module.exports = function RequestHandlerModule(pb) {
*/
RequestHandler.prototype.emitThemeRouteRetrieved = function(cb) {
var context = {
site: this.site,
themeRoute: this.routeTheme,
requestHandler: this
};
Expand Down
7 changes: 5 additions & 2 deletions plugins/sample/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ module.exports = function SamplePluginModule(pb) {
SamplePlugin.onStartupWithContext = function (context, cb) {

/**
* Example for hooking into the RequestHandler for custom control flow
* Example for hooking into the RequestHandler for custom control flow. The context will also provide the site.
* This means that for multi-site implementations where the plugin is installed on a per plugin basis the hook
* should only be registered ONCE. Otherwise it will execute multiple times causing performance to degrade
* @param ctx {object}
* @param {RequestHandler} ctx.requestHandler
* @param {object} ctx.themeRoute
* @param {function} (Error)
*/
pb.RequestHandler.on(pb.RequestHandler.THEME_ROUTE_RETIEVED, function(ctx, callback) {
pb.RequestHandler.on(pb.RequestHandler.THEME_ROUTE_RETIEVED, function (ctx, callback) {
//do what ever needs to be done. Use the callback to continue normal control flow or don't if you need to do redirects
pb.log.debug('SamplePlugin: The request handler hook triggered for request: %s', ctx.requestHandler.url.path);
callback();
});

Expand Down
3 changes: 3 additions & 0 deletions test/include/http/request_handler_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ describe('RequestHandler', function(){
it('should emit the theme route retrieved to the listener and provide the proper context', function(done) {

var themeRoute = {a: '1', b: 2};
var site = 'abc123';
var reqHandler = new RequestHandler(null, {url: '/hello/world', headers: {}});
reqHandler.routeTheme = themeRoute;
reqHandler.site = site;
var cnt = 0;
RequestHandler.on(RequestHandler.THEME_ROUTE_RETIEVED, function(ctx, cb) {
cnt++;
ctx.themeRoute.should.eql(themeRoute);
ctx.requestHandler.should.eql(reqHandler);
ctx.site.should.eql(site);
cb();
});
reqHandler.emitThemeRouteRetrieved(function(err) {
Expand Down

0 comments on commit 605878b

Please sign in to comment.