2017-11-01 15:55:06 +00:00
|
|
|
'use strict';
|
2017-11-09 10:08:11 +00:00
|
|
|
/**
|
|
|
|
* # Router
|
|
|
|
*
|
|
|
|
* A wrapper around express.Router
|
|
|
|
* Intended to be extended anywhere that routes need to be registered in Ghost
|
|
|
|
* Only allows for .use and .get at the moment - we don't have clear use-cases for anything else yet.
|
|
|
|
*/
|
2017-11-01 15:55:06 +00:00
|
|
|
|
2017-11-09 10:08:11 +00:00
|
|
|
var debug = require('ghost-ignition').debug('services:routes:ParentRouter'),
|
|
|
|
express = require('express'),
|
|
|
|
// This is a shared global cache
|
|
|
|
// @TODO expand this as part of the route service
|
|
|
|
routes = [];
|
|
|
|
/**
|
|
|
|
* We expose a very limited amount of express.Router via specialist methods
|
|
|
|
*/
|
2017-11-09 08:43:05 +00:00
|
|
|
class ParentRouter {
|
2017-11-09 10:08:11 +00:00
|
|
|
constructor(name) {
|
|
|
|
this.name = name;
|
|
|
|
this._router = express.Router({mergeParams: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
mountRouter(path, router) {
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
router = path;
|
|
|
|
debug(this.name + ': mountRouter: ' + router.name);
|
|
|
|
this._router.use(router);
|
|
|
|
} else {
|
|
|
|
routes.push(path);
|
|
|
|
debug(this.name + ': mountRouter: ' + router.name + ' at ' + path);
|
|
|
|
this._router.use(path, router);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mountRoute(path, controller) {
|
|
|
|
debug(this.name + ': mountRoute for', path, controller.name);
|
|
|
|
routes.push(path);
|
|
|
|
this._router.get(path, controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
router() {
|
|
|
|
// @TODO: should this just be the handler that is returned?
|
|
|
|
// return this._router.handle.bind(this._router);
|
|
|
|
return this._router;
|
2017-10-31 09:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 08:43:05 +00:00
|
|
|
module.exports = ParentRouter;
|