2015-05-28 10:16:09 -05:00
|
|
|
// # Bootup
|
|
|
|
// This file needs serious love & refactoring
|
|
|
|
|
2016-09-14 09:50:17 -05:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
2013-09-06 10:54:50 -05:00
|
|
|
// Module dependencies
|
2016-10-03 03:33:14 -05:00
|
|
|
var debug = require('debug')('ghost:boot:init'),
|
2017-01-04 11:10:29 -05:00
|
|
|
uuid = require('uuid'),
|
2016-07-15 11:22:41 -05:00
|
|
|
Promise = require('bluebird'),
|
2016-10-17 07:50:29 -05:00
|
|
|
KnexMigrator = require('knex-migrator'),
|
|
|
|
config = require('./config'),
|
2016-11-08 09:21:25 -05:00
|
|
|
logging = require('./logging'),
|
2017-01-26 07:12:00 -05:00
|
|
|
errors = require('./errors'),
|
2016-07-15 11:22:41 -05:00
|
|
|
i18n = require('./i18n'),
|
|
|
|
api = require('./api'),
|
|
|
|
models = require('./models'),
|
2013-12-30 18:13:25 -05:00
|
|
|
permissions = require('./permissions'),
|
2016-07-15 11:22:41 -05:00
|
|
|
apps = require('./apps'),
|
2016-09-30 06:45:59 -05:00
|
|
|
auth = require('./auth'),
|
2016-07-15 11:22:41 -05:00
|
|
|
xmlrpc = require('./data/xml/xmlrpc'),
|
|
|
|
slack = require('./data/slack'),
|
2014-09-19 11:17:58 -05:00
|
|
|
GhostServer = require('./ghost-server'),
|
2016-07-15 11:22:41 -05:00
|
|
|
scheduling = require('./scheduling'),
|
2016-09-09 05:18:09 -05:00
|
|
|
readDirectory = require('./utils/read-directory'),
|
2016-09-09 05:23:47 -05:00
|
|
|
utils = require('./utils'),
|
2016-10-17 10:10:14 -05:00
|
|
|
knexMigrator = new KnexMigrator({
|
|
|
|
knexMigratorFilePath: config.get('paths:appRoot')
|
|
|
|
}),
|
2013-12-06 09:13:15 -05:00
|
|
|
dbHash;
|
2013-09-06 10:54:50 -05:00
|
|
|
|
2013-12-06 09:13:15 -05:00
|
|
|
function initDbHashAndFirstRun() {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
return api.settings.read({key: 'dbHash', context: {internal: true}}).then(function (response) {
|
2014-04-27 18:28:50 -05:00
|
|
|
var hash = response.settings[0].value,
|
|
|
|
initHash;
|
|
|
|
|
|
|
|
dbHash = hash;
|
2013-12-16 05:16:06 -05:00
|
|
|
|
|
|
|
if (dbHash === null) {
|
2014-04-27 18:28:50 -05:00
|
|
|
initHash = uuid.v4();
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 07:41:19 -05:00
|
|
|
return api.settings.edit({settings: [{key: 'dbHash', value: initHash}]}, {context: {internal: true}})
|
|
|
|
.then(function (response) {
|
|
|
|
dbHash = response.settings[0].value;
|
|
|
|
return dbHash;
|
2015-08-26 09:10:23 -05:00
|
|
|
// Use `then` here to do 'first run' actions
|
|
|
|
});
|
2013-12-16 05:16:06 -05:00
|
|
|
}
|
2014-05-04 13:32:37 -05:00
|
|
|
|
2014-04-27 18:28:50 -05:00
|
|
|
return dbHash;
|
2013-12-06 09:13:15 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-28 10:16:09 -05:00
|
|
|
// ## Initialise Ghost
|
|
|
|
// Sets up the express server instances, runs init on a bunch of stuff, configures views, helpers, routes and more
|
2014-08-19 11:36:46 -05:00
|
|
|
// Finally it returns an instance of GhostServer
|
2014-08-23 11:19:13 -05:00
|
|
|
function init(options) {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Init Start...');
|
2016-07-15 11:22:41 -05:00
|
|
|
options = options || {};
|
|
|
|
|
2016-09-30 06:45:59 -05:00
|
|
|
var ghostServer, parentApp;
|
2016-05-19 06:49:22 -05:00
|
|
|
|
2013-12-06 09:13:15 -05:00
|
|
|
// ### Initialisation
|
2014-02-19 22:22:02 -05:00
|
|
|
// The server and its dependencies require a populated config
|
|
|
|
// It returns a promise that is resolved when the application
|
|
|
|
// has finished starting up.
|
2014-01-02 15:27:09 -05:00
|
|
|
|
2015-11-12 07:29:45 -05:00
|
|
|
// Initialize Internationalization
|
|
|
|
i18n.init();
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('I18n done');
|
2015-11-12 07:29:45 -05:00
|
|
|
|
2016-09-13 15:24:57 -05:00
|
|
|
return readDirectory(config.getContentPath('apps')).then(function loadThemes(result) {
|
2016-09-13 14:38:16 -05:00
|
|
|
config.set('paths:availableApps', result);
|
2016-09-09 05:10:43 -05:00
|
|
|
return api.themes.loadThemes();
|
2014-08-23 11:19:13 -05:00
|
|
|
}).then(function () {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Themes & apps done');
|
|
|
|
|
2016-03-01 23:42:01 -05:00
|
|
|
models.init();
|
2016-10-17 07:50:29 -05:00
|
|
|
}).then(function () {
|
2017-01-26 07:12:00 -05:00
|
|
|
return knexMigrator.isDatabaseOK()
|
|
|
|
.catch(function (outerErr) {
|
|
|
|
if (outerErr.code === 'DB_NOT_INITIALISED') {
|
|
|
|
throw outerErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: migration table does not exist, figure out if database is compatible
|
|
|
|
return models.Settings.findOne({key: 'databaseVersion', context: {internal: true}})
|
|
|
|
.then(function (response) {
|
|
|
|
// CASE: no db version key, database is compatible
|
|
|
|
if (!response) {
|
|
|
|
throw outerErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new errors.DatabaseVersionError({
|
|
|
|
message: 'Your database version is not compatible with Ghost 1.0.0 Alpha (master branch)',
|
|
|
|
context: 'Want to keep your DB? Use Ghost < 1.0.0 or the "stable" branch. Otherwise please delete your DB and restart Ghost.',
|
|
|
|
help: 'More information on the Ghost 1.0.0 Alpha at https://support.ghost.org/v1-0-alpha'
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
// CASE: settings table does not exist
|
|
|
|
if (err.errno === 1 || err.errno === 1146) {
|
|
|
|
throw outerErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
});
|
2014-05-15 21:29:42 -05:00
|
|
|
}).then(function () {
|
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
2013-12-06 09:13:15 -05:00
|
|
|
}).then(function () {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Models & database done');
|
2016-11-08 08:37:19 -05:00
|
|
|
|
|
|
|
return api.settings.updateSettingsCache();
|
2014-05-06 19:49:25 -05:00
|
|
|
}).then(function () {
|
2016-11-08 08:37:19 -05:00
|
|
|
debug('Update settings cache done');
|
2014-05-06 19:49:25 -05:00
|
|
|
// Initialize the permissions actions and objects
|
2014-09-02 22:15:15 -05:00
|
|
|
// NOTE: Must be done before initDbHashAndFirstRun calls
|
2014-05-06 19:49:25 -05:00
|
|
|
return permissions.init();
|
2013-12-06 09:13:15 -05:00
|
|
|
}).then(function () {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Permissions done');
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.join(
|
2013-12-06 09:13:15 -05:00
|
|
|
// Check for or initialise a dbHash.
|
|
|
|
initDbHashAndFirstRun(),
|
2014-02-19 22:22:02 -05:00
|
|
|
// Initialize apps
|
2014-10-27 19:41:18 -05:00
|
|
|
apps.init(),
|
2015-03-24 15:23:23 -05:00
|
|
|
// Initialize xmrpc ping
|
2016-06-05 06:22:11 -05:00
|
|
|
xmlrpc.listen(),
|
2016-03-29 03:40:44 -05:00
|
|
|
// Initialize slack ping
|
2016-06-05 06:22:11 -05:00
|
|
|
slack.listen()
|
2013-12-06 09:13:15 -05:00
|
|
|
);
|
2014-01-14 18:14:44 -05:00
|
|
|
}).then(function () {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Apps, XMLRPC, Slack done');
|
2016-10-06 07:27:35 -05:00
|
|
|
|
🎉 🎨 ✨ Remove middleware/index.js (#7548)
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484
* 🎨 Co-locate all admin-related code in /admin
- move all the admin related code from controllers, routes and helpers into a single location
- add error handling middleware explicitly to adminApp
- re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp
- TODO: rethink the structure of /admin, this should probably be an internal app
* 💄 Group global middleware together
- There are only a few pieces of middleware which are "global"
- These are needed for the admin, blog and api
- Everything else is only needed in one or two places
* ✨ Introduce a separate blogApp
- create a brand-new blogApp
- mount all blog/theme only middleware etc onto blogApp
- mount error handling on blogApp only
* 🎨 Separate error handling for HTML & API JSON
- split JSON and HTML error handling into separate functions
- re-introduce a way to not output the stack for certain errors
- add more tests around errors & an assertion framework for checking JSON Errors
- TODO: better 404 handling for static assets
Rationale:
The API is very different to the blog/admin panel:
- It is intended to only ever serve JSON, never HTML responses
- It is intended to always serve JSON
Meanwhile the blog and admin panel have no need for JSON errors,
when an error happens on those pages, we should serve HTML pages
which are nicely formatted with the error & using the correct template
* 🐛 Fix checkSSL to work for subapps
- in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url`
* 🔥 Get rid of decide-is-admin (part 1/2)
- delete decide-is-admin & tests
- add two small functions to apiApp and adminApp to set res.isAdmin
- mount checkSSL on all the apps
- TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D
- PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name
Rationale:
Now that we have both an adminApp and an apiApp,
we can temporarily replace this weird path-matching middleware
with middleware that sets res.isAdmin for api & admin
* 🎨 Wire up prettyURLs on all Apps
- prettyURLs is needed for all requests
- it cannot be global because it has to live after asset middleware, and before routing
- this does not result in duplicate redirects, but does result in duplicate checks
- TODO: resolve extra middleware in stack by making blogApp a sub app
* ⏱ Add debug to API setup
* 🎨 Rename blogApp -> parentApp in middleware
* 🎨 Co-locate all blog-related code in /blog
- Move all of the blogApp code from middleware/index.js to blog/app.js
- Move routes/frontend.js to blog/routes.js
- Remove the routes/index.js and routes folder, this is empty now!
- @TODO is blog the best name for this? 🤔
- @TODO sort out the big hunk of asset-related mess
- @TODO also separate out the concept of theme from blog
* 🎉 Replace middleware index with server/app.js
- The final piece of the puzzle! 🎉 🎈 🎂
- We no longer have our horrendous middleware/index.js
- Instead, we have a set of app.js files, which all use a familiar pattern
* 💄 Error handling fixups
2016-10-13 10:24:09 -05:00
|
|
|
// Setup our collection of express apps
|
|
|
|
parentApp = require('./app')();
|
2016-10-06 07:27:35 -05:00
|
|
|
|
🎉 🎨 ✨ Remove middleware/index.js (#7548)
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484
* 🎨 Co-locate all admin-related code in /admin
- move all the admin related code from controllers, routes and helpers into a single location
- add error handling middleware explicitly to adminApp
- re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp
- TODO: rethink the structure of /admin, this should probably be an internal app
* 💄 Group global middleware together
- There are only a few pieces of middleware which are "global"
- These are needed for the admin, blog and api
- Everything else is only needed in one or two places
* ✨ Introduce a separate blogApp
- create a brand-new blogApp
- mount all blog/theme only middleware etc onto blogApp
- mount error handling on blogApp only
* 🎨 Separate error handling for HTML & API JSON
- split JSON and HTML error handling into separate functions
- re-introduce a way to not output the stack for certain errors
- add more tests around errors & an assertion framework for checking JSON Errors
- TODO: better 404 handling for static assets
Rationale:
The API is very different to the blog/admin panel:
- It is intended to only ever serve JSON, never HTML responses
- It is intended to always serve JSON
Meanwhile the blog and admin panel have no need for JSON errors,
when an error happens on those pages, we should serve HTML pages
which are nicely formatted with the error & using the correct template
* 🐛 Fix checkSSL to work for subapps
- in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url`
* 🔥 Get rid of decide-is-admin (part 1/2)
- delete decide-is-admin & tests
- add two small functions to apiApp and adminApp to set res.isAdmin
- mount checkSSL on all the apps
- TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D
- PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name
Rationale:
Now that we have both an adminApp and an apiApp,
we can temporarily replace this weird path-matching middleware
with middleware that sets res.isAdmin for api & admin
* 🎨 Wire up prettyURLs on all Apps
- prettyURLs is needed for all requests
- it cannot be global because it has to live after asset middleware, and before routing
- this does not result in duplicate redirects, but does result in duplicate checks
- TODO: resolve extra middleware in stack by making blogApp a sub app
* ⏱ Add debug to API setup
* 🎨 Rename blogApp -> parentApp in middleware
* 🎨 Co-locate all blog-related code in /blog
- Move all of the blogApp code from middleware/index.js to blog/app.js
- Move routes/frontend.js to blog/routes.js
- Remove the routes/index.js and routes folder, this is empty now!
- @TODO is blog the best name for this? 🤔
- @TODO sort out the big hunk of asset-related mess
- @TODO also separate out the concept of theme from blog
* 🎉 Replace middleware index with server/app.js
- The final piece of the puzzle! 🎉 🎈 🎂
- We no longer have our horrendous middleware/index.js
- Instead, we have a set of app.js files, which all use a familiar pattern
* 💄 Error handling fixups
2016-10-13 10:24:09 -05:00
|
|
|
debug('Express Apps done');
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2016-11-08 09:21:25 -05:00
|
|
|
// runs asynchronous
|
|
|
|
auth.init({
|
2016-11-07 06:38:05 -05:00
|
|
|
authType: config.get('auth:type'),
|
|
|
|
ghostAuthUrl: config.get('auth:url'),
|
2017-02-02 13:51:35 -05:00
|
|
|
redirectUri: utils.url.urlFor('admin', true),
|
|
|
|
clientUri: utils.url.urlFor('home', true),
|
2017-02-03 08:15:11 -05:00
|
|
|
clientName: api.settings.cache.get('title'),
|
|
|
|
clientDescription: api.settings.cache.get('description')
|
2016-11-07 06:38:05 -05:00
|
|
|
}).then(function (response) {
|
|
|
|
parentApp.use(response.auth);
|
2016-11-08 09:21:25 -05:00
|
|
|
}).catch(function onAuthError(err) {
|
|
|
|
logging.error(err);
|
2016-11-07 06:38:05 -05:00
|
|
|
});
|
2016-09-30 06:45:59 -05:00
|
|
|
}).then(function () {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Auth done');
|
2016-05-14 13:02:45 -05:00
|
|
|
return new GhostServer(parentApp);
|
2016-05-19 06:49:22 -05:00
|
|
|
}).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
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Server done');
|
2016-09-13 14:38:16 -05:00
|
|
|
return scheduling.init({
|
2016-10-25 06:19:22 -05:00
|
|
|
schedulerUrl: config.get('scheduling').schedulerUrl,
|
2016-09-13 14:38:16 -05:00
|
|
|
active: config.get('scheduling').active,
|
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937)
* 🔥 kill apiUrl helper, use urlFor helper instead
More consistency of creating urls.
Creates an easier ability to add config changes.
Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs.
The url util need's refactoring anyway.
* 🔥 urlSSL
Remove all urlSSL usages.
Add TODO's for the next commit to re-add logic for deleted logic.
e.g.
- cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available
- theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag)
The changes in this commit doesn't have to be right, but it helped going step by step.
The next commit is the more interesting one.
* 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic
I wanted to remove the forceAdminSSL as separate commit, but was hard to realise.
That's why both changes are in one commit:
1. remove forceAdminSSL
2. add admin.url option
- fix TODO's from last commits
- rewrite the ssl middleware!
- create some private helper functions in the url helper to realise the changes
- rename some wordings and functions e.g. base === blog (we have so much different wordings)
- i would like to do more, but this would end in a non readable PR
- this commit contains the most important changes to offer admin.url option
* 🤖 adapt tests
IMPORTANT
- all changes in the routing tests were needed, because each routing test did not start the ghost server
- they just required the ghost application, which resulted in a random server port
- having a random server port results in a redirect, caused by the ssl/redirect middleware
* 😎 rename check-ssl middleware
* 🎨 fix theme-handler because of master rebase
2017-02-03 13:13:22 -05:00
|
|
|
apiUrl: utils.url.urlFor('api', true),
|
2016-09-13 15:24:57 -05:00
|
|
|
internalPath: config.get('paths').internalSchedulingPath,
|
|
|
|
contentPath: config.getContentPath('scheduling')
|
2016-09-13 14:38:16 -05:00
|
|
|
});
|
2016-05-19 06:49:22 -05:00
|
|
|
}).then(function () {
|
2016-10-03 03:33:14 -05:00
|
|
|
debug('Scheduling done');
|
|
|
|
debug('...Init End');
|
2016-05-19 06:49:22 -05:00
|
|
|
return ghostServer;
|
2014-02-19 22:22:02 -05:00
|
|
|
});
|
2013-11-23 12:54:47 -05:00
|
|
|
}
|
|
|
|
|
2013-11-17 13:40:26 -05:00
|
|
|
module.exports = init;
|