Skip to content

Commit

Permalink
Calls legacyController if present (#681)
Browse files Browse the repository at this point in the history
Restores routing API removed here 79f75b8
Site controllers can export a function called legacyController which
amphora calls with the router and renderer.
Authored by: felkerch
  • Loading branch information
felkerch authored and james-owen committed Dec 9, 2019
1 parent 1b6ceca commit d1b0f09
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
22 changes: 16 additions & 6 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash'),
path = require('path'),
siteService = require('./services/sites'),
attachRoutes = require('./services/attachRoutes'),
render = require('./render'),
responses = require('./responses'),
files = require('./files'),
plugins = require('./services/plugins'),
Expand Down Expand Up @@ -120,6 +121,20 @@ function addCORS(site) {
};
}

function setRoutes(controller, router, site) {
if (_.isFunction(controller.legacyController)) {
controller.legacyController(router, render, site);
}

if (Array.isArray(controller.routes)) {
attachRoutes(router, controller.routes, site);
}

if (!controller.legacyController && !controller.routes) {
log('warn', `There is no router for site: ${site.slug}`);
}
}

/**
* Default way to load site controllers.
*
Expand Down Expand Up @@ -154,12 +169,7 @@ function addSiteController(router, site, providers) {
router.use(controller.middleware);
}

if (Array.isArray(controller.routes)) {
attachRoutes(router, controller.routes, site);
} else {
log('warn', `There is no router for site: ${site.slug}`);
}

setRoutes(controller, router, site);
// Providers are the same across all sites, but this is a convenient
// place to store it so that it's available everywhere
_.set(site, 'providers', providers);
Expand Down
13 changes: 9 additions & 4 deletions lib/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,24 @@ describe(_.startCase(filename), function () {
fn(router, site);
});

it('does nothing if site controller is not a function', function () {
const siteDir = 'some-location',
it('calls legacyController if defined', function () {
const controller = sandbox.stub(),
siteDir = 'some-location',
router = {},
site = { dir: siteDir };

siteService.getParentSite.returns(_.cloneDeep(site));
sandbox.stub(files, 'tryRequire');

files.tryRequire.returns({});
files.tryRequire.returns({
legacyController: controller
});

fn(router, site);

sinon.assert.called(controller);
});


it('calls `attachRoutes` if the `routes` Array is defined', function () {
const siteDir = 'some-location',
paths = [],
Expand Down

0 comments on commit d1b0f09

Please sign in to comment.