Ghost/core/server/index.js
Katharina Irrgang b392d1925a
Dynamic Routing Beta (#9596)
refs #9601

### Dynamic Routing

This is the beta version of dynamic routing. 

- we had a initial implementation of "channels" available in the codebase
- we have removed and moved this implementation 
- there is now a centralised place for dynamic routing - server/services/routing
- each routing component is represented by a router type e.g. collections, routes, static pages, taxonomies, rss, preview of posts
- keep as much as possible logic of routing helpers, middlewares and controllers
- ensure test coverage
- connect all the things together
  - yaml file + validation
  - routing + routers
  - url service
  - sitemaps
  - url access
- deeper implementation of yaml validations
  - e.g. hard require slashes
- ensure routing hierarchy/order
  - e.g. you enable the subscriber app
  - you have a custom static page, which lives under the same slug /subscribe
  - static pages are stronger than apps
  - e.g. the first collection owns the post it has filtered
  - a post cannot live in two collections
- ensure apps are still working and hook into the routers layer (or better said: and register in the routing service)
- put as much as possible comments to the code base for better understanding
- ensure a clean debug log
- ensure we can unmount routes
  - e.g. you have a collection permalink of /:slug/ represented by {globals.permalink}
  - and you change the permalink in the admin to dated permalink
  - the express route get's refreshed from /:slug/ to /:year/:month/:day/:slug/
  - unmount without server restart, yey
- ensure we are backwards compatible
  - e.g. render home.hbs for collection index if collection route is /
  - ensure you can access your configured permalink from the settings table with {globals.permalink}

### Render 503 if url service did not finish

- return 503 if the url service has not finished generating the resource urls

### Rewrite sitemaps

- we have rewritten the sitemaps "service", because the url generator does no longer happen on runtime
- we generate all urls on bootstrap
- the sitemaps service will consume created resource and router urls
- these urls will be shown on the xml pages
- we listen on url events
- we listen on router events
- we no longer have to fetch the resources, which is nice
  - the urlservice pre-fetches resources and emits their urls
- the urlservice is the only component who knows which urls are valid
- i made some ES6 adaptions
- we keep the caching logic -> only regenerate xml if there is a change
- updated tests
- checked test coverage (100%)

### Re-work usage of Url utility

- replace all usages of `urlService.utils.urlFor` by `urlService.getByResourceId`
  - only for resources e.g. post, author, tag
- this is important, because with dynamic routing we no longer create static urls based on the settings permalink on runtime
- adapt url utility
- adapt tests
2018-06-05 19:02:20 +02:00

127 lines
4.2 KiB
JavaScript

// # Bootup
// This file needs serious love & refactoring
/**
* make sure overrides get's called first!
* - keeping the overrides require here works for installing Ghost as npm!
*
* the call order is the following:
* - root index requires core module
* - core index requires server
* - overrides is the first package to load
*/
require('./overrides');
// Module dependencies
var debug = require('ghost-ignition').debug('boot:init'),
config = require('./config'),
Promise = require('bluebird'),
common = require('./lib/common'),
models = require('./models'),
permissions = require('./services/permissions'),
auth = require('./services/auth'),
dbHealth = require('./data/db/health'),
GhostServer = require('./ghost-server'),
scheduling = require('./adapters/scheduling'),
settings = require('./services/settings'),
themes = require('./services/themes'),
urlService = require('./services/url'),
// Services that need initialisation
apps = require('./services/apps'),
xmlrpc = require('./services/xmlrpc'),
slack = require('./services/slack'),
webhooks = require('./services/webhooks');
// ## Initialise Ghost
function init() {
debug('Init Start...');
var ghostServer, parentApp;
// Initialize default internationalization, just for core now
// (settings for language and theme not yet available here)
common.i18n.init();
debug('Default i18n done for core');
models.init();
debug('models done');
return dbHealth.check().then(function () {
debug('DB health check done');
// Populate any missing default settings
// Refresh the API settings cache
return settings.init();
}).then(function () {
debug('Update settings cache done');
common.events.emit('db.ready');
// Full internationalization for core could be here
// in a future version with backend translations
// (settings for language and theme available here;
// internationalization for theme is done
// shortly after, when activating the theme)
//
// Initialize the permissions actions and objects
return permissions.init();
}).then(function () {
debug('Permissions done');
return Promise.join(
themes.init(),
// Initialize xmrpc ping
xmlrpc.listen(),
// Initialize slack ping
slack.listen(),
// Initialize webhook pings
webhooks.listen()
);
}).then(function () {
debug('Apps, XMLRPC, Slack done');
// Setup our collection of express apps
parentApp = require('./web/parent-app')();
// Initialise analytics events
if (config.get('segment:key')) {
require('./analytics-events').init();
}
debug('Express Apps done');
}).then(function () {
/**
* @NOTE:
*
* Must happen after express app bootstrapping, because we need to ensure that all
* routers are created and are now ready to register additional routes. In this specific case, we
* are waiting that the AppRouter was instantiated. And then we can register e.g. amp if enabled.
*
* If you create a published post, the url is always stronger than any app url, which is equal.
*/
return apps.init();
}).then(function () {
parentApp.use(auth.init());
debug('Auth done');
return new GhostServer(parentApp);
}).then(function (_ghostServer) {
ghostServer = _ghostServer;
// scheduling can trigger api requests, that's why we initialize the module after the ghost server creation
// scheduling module can create x schedulers with different adapters
debug('Server done');
return scheduling.init({
schedulerUrl: config.get('scheduling').schedulerUrl,
active: config.get('scheduling').active,
apiUrl: urlService.utils.urlFor('api', true),
internalPath: config.get('paths').internalSchedulingPath,
contentPath: config.getContentPath('scheduling')
});
}).then(function () {
debug('Scheduling done');
debug('...Init End');
return ghostServer;
});
}
module.exports = init;