2013-11-20 06:43:55 +04:00
|
|
|
// If no env is set, default to development
|
|
|
|
// This needs to be above all other require()
|
|
|
|
// modules to ensure config gets right setting.
|
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// Module dependencies
|
2013-12-31 03:13:25 +04:00
|
|
|
var crypto = require('crypto'),
|
|
|
|
express = require('express'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
fs = require('fs'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
path = require('path'),
|
|
|
|
Polyglot = require('node-polyglot'),
|
|
|
|
semver = require('semver'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
when = require('when'),
|
|
|
|
|
|
|
|
api = require('./api'),
|
|
|
|
config = require('./config'),
|
|
|
|
errors = require('./errorHandling'),
|
|
|
|
helpers = require('./helpers'),
|
|
|
|
mailer = require('./mail'),
|
|
|
|
middleware = require('./middleware'),
|
|
|
|
models = require('./models'),
|
|
|
|
permissions = require('./permissions'),
|
|
|
|
plugins = require('./plugins'),
|
|
|
|
routes = require('./routes'),
|
|
|
|
packageInfo = require('../../package.json'),
|
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
|
|
|
|
// Variables
|
2013-12-06 18:13:15 +04:00
|
|
|
dbHash;
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-10-07 17:28:53 +04:00
|
|
|
// If we're in development mode, require "when/console/monitor"
|
2013-11-25 23:20:01 +04:00
|
|
|
// for help in seeing swallowed promise errors, and log any
|
|
|
|
// stderr messages from bluebird promises.
|
2013-10-07 17:28:53 +04:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
require('when/monitor/console');
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:13:15 +04:00
|
|
|
function doFirstRun() {
|
|
|
|
var firstRunMessage = [
|
|
|
|
'Welcome to Ghost.',
|
|
|
|
'You\'re running under the <strong>',
|
|
|
|
process.env.NODE_ENV,
|
|
|
|
'</strong>environment.',
|
|
|
|
|
|
|
|
'Your URL is set to',
|
|
|
|
'<strong>' + config().url + '</strong>.',
|
|
|
|
'See <a href="http://docs.ghost.org/">http://docs.ghost.org</a> for instructions.'
|
|
|
|
];
|
|
|
|
|
|
|
|
return api.notifications.add({
|
|
|
|
type: 'info',
|
|
|
|
message: firstRunMessage.join(' '),
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'ghost-first-run'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDbHashAndFirstRun() {
|
2013-12-16 14:16:06 +04:00
|
|
|
return when(api.settings.read('dbHash')).then(function (hash) {
|
2013-12-06 18:13:15 +04:00
|
|
|
// we already ran this, chill
|
|
|
|
// Holds the dbhash (mainly used for cookie secret)
|
2013-12-16 14:16:06 +04:00
|
|
|
dbHash = hash.value;
|
|
|
|
|
|
|
|
if (dbHash === null) {
|
|
|
|
var initHash = uuid.v4();
|
|
|
|
return when(api.settings.edit('dbHash', initHash)).then(function (settings) {
|
|
|
|
dbHash = settings.dbHash;
|
|
|
|
return dbHash;
|
|
|
|
}).then(doFirstRun);
|
|
|
|
}
|
|
|
|
return dbHash.value;
|
2013-12-06 18:13:15 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Sets up the express server instance.
|
|
|
|
// Instantiates the ghost singleton,
|
|
|
|
// helpers, routes, middleware, and plugins.
|
|
|
|
// Finally it starts the http server.
|
|
|
|
function setup(server) {
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-31 03:13:25 +04:00
|
|
|
// create a hash for cache busting assets
|
2014-01-07 21:19:14 +04:00
|
|
|
var assetHash = (crypto.createHash('md5').update(packageInfo.version + Date.now()).digest('hex')).substring(0, 10);
|
2013-12-31 03:13:25 +04:00
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
// Set up Polygot instance on the require module
|
|
|
|
Polyglot.instance = new Polyglot();
|
|
|
|
|
2013-12-06 18:13:15 +04:00
|
|
|
// ### Initialisation
|
2014-01-03 00:27:09 +04:00
|
|
|
|
|
|
|
// Initialise the models
|
|
|
|
models.init().then(function () {
|
2013-12-06 18:13:15 +04:00
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
|
|
|
}).then(function () {
|
|
|
|
// Initialize the settings cache
|
|
|
|
return api.init();
|
|
|
|
}).then(function () {
|
2013-12-10 08:41:58 +04:00
|
|
|
// We must pass the api.settings object
|
2013-12-06 18:13:15 +04:00
|
|
|
// into this method due to circular dependencies.
|
2013-12-26 16:15:10 +04:00
|
|
|
return config.theme.update(api.settings, config().url);
|
2013-12-06 18:13:15 +04:00
|
|
|
}).then(function () {
|
|
|
|
return when.join(
|
|
|
|
// Check for or initialise a dbHash.
|
|
|
|
initDbHashAndFirstRun(),
|
|
|
|
// Initialize the permissions actions and objects
|
|
|
|
permissions.init()
|
|
|
|
);
|
|
|
|
}).then(function () {
|
2013-12-14 20:28:54 +04:00
|
|
|
// Initialize mail
|
2013-12-02 03:31:55 +04:00
|
|
|
return mailer.init();
|
2013-11-20 06:43:55 +04:00
|
|
|
}).then(function () {
|
2013-12-14 20:28:54 +04:00
|
|
|
var adminHbs = hbs.create();
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ##Configuration
|
2013-12-31 03:13:25 +04:00
|
|
|
server.set('version hash', assetHash);
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// return the correct mime type for woff filess
|
|
|
|
express['static'].mime.define({'application/font-woff': ['woff']});
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// ## View engine
|
|
|
|
// set the view engine
|
|
|
|
server.set('view engine', 'hbs');
|
|
|
|
|
|
|
|
// Create a hbs instance for admin and init view engine
|
|
|
|
server.set('admin view engine', adminHbs.express3({partialsDir: config.paths().adminViews + 'partials'}));
|
|
|
|
|
|
|
|
// Load helpers
|
2013-12-31 03:13:25 +04:00
|
|
|
helpers.loadCoreHelpers(adminHbs, assetHash);
|
2013-12-02 03:31:55 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ## Middleware
|
2013-12-06 18:13:15 +04:00
|
|
|
middleware(server, dbHash);
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ## Routing
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Set up API routes
|
|
|
|
routes.api(server);
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Set up Admin routes
|
|
|
|
routes.admin(server);
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Set up Frontend routes
|
|
|
|
routes.frontend(server);
|
|
|
|
|
|
|
|
// Are we using sockets? Custom socket or the default?
|
|
|
|
function getSocket() {
|
2013-11-20 17:58:52 +04:00
|
|
|
if (config().server.hasOwnProperty('socket')) {
|
2013-12-09 04:49:02 +04:00
|
|
|
return _.isString(config().server.socket) ? config().server.socket : path.join(config.path().contentPath, process.env.NODE_ENV + '.socket');
|
2013-11-20 06:43:55 +04:00
|
|
|
}
|
|
|
|
return false;
|
2013-10-09 20:07:43 +04:00
|
|
|
}
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
function startGhost() {
|
|
|
|
// Tell users if their node version is not supported, and exit
|
|
|
|
if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) {
|
2013-09-06 19:54:50 +04:00
|
|
|
console.log(
|
2013-11-20 06:43:55 +04:00
|
|
|
"\nERROR: Unsupported version of Node".red,
|
|
|
|
"\nGhost needs Node version".red,
|
|
|
|
packageInfo.engines.node.yellow,
|
|
|
|
"you are using version".red,
|
|
|
|
process.versions.node.yellow,
|
2013-12-12 20:47:35 +04:00
|
|
|
"\nPlease go to http://nodejs.org to get a supported version".green
|
2013-09-06 19:54:50 +04:00
|
|
|
);
|
2013-11-20 06:43:55 +04:00
|
|
|
|
2013-09-15 15:11:47 +04:00
|
|
|
process.exit(0);
|
2013-11-20 06:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Startup & Shutdown messages
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2013-09-15 15:11:47 +04:00
|
|
|
console.log(
|
2013-11-20 06:43:55 +04:00
|
|
|
"Ghost is running...".green,
|
|
|
|
"\nYour blog is now available on",
|
2013-11-20 17:58:52 +04:00
|
|
|
config().url,
|
2013-11-20 06:43:55 +04:00
|
|
|
"\nCtrl+C to shut down".grey
|
2013-09-15 15:11:47 +04:00
|
|
|
);
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ensure that Ghost exits correctly on Ctrl+C
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
console.log(
|
|
|
|
"\nGhost has shut down".red,
|
|
|
|
"\nYour blog is now offline"
|
|
|
|
);
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
("Ghost is running in " + process.env.NODE_ENV + "...").green,
|
|
|
|
"\nListening on",
|
2013-11-20 17:58:52 +04:00
|
|
|
getSocket() || config().server.host + ':' + config().server.port,
|
2013-11-20 06:43:55 +04:00
|
|
|
"\nUrl configured as:",
|
2013-11-20 17:58:52 +04:00
|
|
|
config().url,
|
2013-11-20 06:43:55 +04:00
|
|
|
"\nCtrl+C to shut down".grey
|
|
|
|
);
|
|
|
|
// ensure that Ghost exits correctly on Ctrl+C
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
console.log(
|
|
|
|
"\nGhost has shutdown".red,
|
|
|
|
"\nGhost was running for",
|
|
|
|
Math.round(process.uptime()),
|
|
|
|
"seconds"
|
|
|
|
);
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
2013-09-15 01:34:12 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
}
|
2013-09-15 01:34:12 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Initialize plugins then start the server
|
2013-12-06 18:13:15 +04:00
|
|
|
plugins.init().then(function () {
|
2013-11-20 06:43:55 +04:00
|
|
|
|
|
|
|
// ## Start Ghost App
|
|
|
|
if (getSocket()) {
|
|
|
|
// Make sure the socket is gone before trying to create another
|
|
|
|
fs.unlink(getSocket(), function (err) {
|
|
|
|
/*jslint unparam:true*/
|
|
|
|
server.listen(
|
|
|
|
getSocket(),
|
|
|
|
startGhost
|
|
|
|
);
|
2013-12-30 16:56:09 +04:00
|
|
|
fs.chmod(getSocket(), '0660');
|
2013-11-20 06:43:55 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
2013-09-15 01:34:12 +04:00
|
|
|
server.listen(
|
2013-11-20 17:58:52 +04:00
|
|
|
config().server.port,
|
|
|
|
config().server.host,
|
2013-09-15 01:34:12 +04:00
|
|
|
startGhost
|
|
|
|
);
|
2013-11-20 06:43:55 +04:00
|
|
|
}
|
2013-10-09 20:07:43 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
errors.logErrorAndExit(err);
|
2013-09-15 01:34:12 +04:00
|
|
|
});
|
2013-11-20 06:43:55 +04:00
|
|
|
}
|
|
|
|
|
2013-11-23 21:54:47 +04:00
|
|
|
// Initializes the ghost application.
|
|
|
|
function init(app) {
|
|
|
|
if (!app) {
|
|
|
|
app = express();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The server and its dependencies require a populated config
|
|
|
|
setup(app);
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:40:26 +04:00
|
|
|
module.exports = init;
|