0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/services/routing/controllers/static.js
kirrg001 c2fa469c4d Dynamic Routing Beta: Channels
refs #9601

- refactor architecture of routing so you can define a channel
- a channel is a different way of looking at your posts (a view)
- a channel does not change the url of a resource

Example channel

```
routes:
  /worldcup-2018-russia/:
    controller: channel
    filter: tag:football18
    data: tag.football18
```

- added ability to redirect resources to a channel/static route
- support templates for channels
- ensure we still support static routes (e.g. /about/: home)
- ensure pagination + rss works out of the box
2018-06-24 02:06:58 +02:00

47 lines
1.5 KiB
JavaScript

const _ = require('lodash'),
Promise = require('bluebird'),
debug = require('ghost-ignition').debug('services:routing:controllers:static'),
api = require('../../../api'),
helpers = require('../helpers');
function processQuery(query) {
query = _.cloneDeep(query);
// Return a promise for the api query
return api[query.resource][query.type](query.options);
}
module.exports = function staticController(req, res, next) {
debug('staticController', res.locals.routerOptions);
let props = {};
_.each(res.locals.routerOptions.data, function (query, name) {
props[name] = processQuery(query);
});
return Promise.props(props)
.then(function handleResult(result) {
let response = {};
if (res.locals.routerOptions.data) {
response.data = {};
_.each(res.locals.routerOptions.data, function (config, name) {
if (config.type === 'browse') {
response.data[name] = result[name];
} else {
response.data[name] = result[name][config.resource];
}
});
}
// @TODO: get rid of this O_O
_.each(response.data, function (data) {
helpers.secure(req, data);
});
helpers.renderer(req, res, helpers.formatResponse.entries(response));
})
.catch(helpers.handleError(next));
};