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-11-26 07:22:59 +04:00
|
|
|
var config = require('./config'),
|
2013-11-20 17:58:52 +04:00
|
|
|
express = require('express'),
|
2013-11-20 06:43:55 +04:00
|
|
|
when = require('when'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
semver = require('semver'),
|
|
|
|
fs = require('fs'),
|
2013-11-26 07:22:59 +04:00
|
|
|
errors = require('./errorHandling'),
|
|
|
|
plugins = require('./plugins'),
|
2013-11-20 06:43:55 +04:00
|
|
|
path = require('path'),
|
2013-11-28 06:45:01 +04:00
|
|
|
Polyglot = require('node-polyglot'),
|
|
|
|
mailer = require('./mail'),
|
2013-11-26 07:22:59 +04:00
|
|
|
helpers = require('./helpers'),
|
|
|
|
middleware = require('./middleware'),
|
|
|
|
routes = require('./routes'),
|
|
|
|
packageInfo = require('../../package.json'),
|
2013-12-06 18:13:15 +04:00
|
|
|
models = require('./models'),
|
|
|
|
permissions = require('./permissions'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
api = require('./api'),
|
2013-09-06 19:54:50 +04:00
|
|
|
|
|
|
|
// Variables
|
2013-11-23 21:54:47 +04:00
|
|
|
setup,
|
2013-12-06 18:13:15 +04:00
|
|
|
init,
|
|
|
|
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() {
|
|
|
|
return when(models.Settings.read('dbHash')).then(function (hash) {
|
|
|
|
// we already ran this, chill
|
|
|
|
// Holds the dbhash (mainly used for cookie secret)
|
|
|
|
dbHash = hash.attributes.value;
|
|
|
|
return dbHash;
|
|
|
|
}).otherwise(function (error) {
|
|
|
|
/*jslint unparam:true*/
|
|
|
|
// this is where all the "first run" functionality should go
|
|
|
|
var hash = uuid.v4();
|
|
|
|
return when(models.Settings.add({key: 'dbHash', value: hash, type: 'core'})).then(function () {
|
|
|
|
dbHash = hash;
|
|
|
|
return dbHash;
|
|
|
|
}).then(doFirstRun);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// Set up Polygot instance on the require module
|
|
|
|
Polyglot.instance = new Polyglot();
|
|
|
|
|
2013-12-06 18:13:15 +04:00
|
|
|
// ### Initialisation
|
|
|
|
when.join(
|
|
|
|
// Initialise the models
|
|
|
|
models.init(),
|
|
|
|
// Calculate paths
|
|
|
|
config.paths.updatePaths(config().url)
|
|
|
|
).then(function () {
|
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
|
|
|
}).then(function () {
|
|
|
|
// Initialize the settings cache
|
|
|
|
return api.init();
|
|
|
|
}).then(function () {
|
|
|
|
// We must pass the api and config object
|
|
|
|
// into this method due to circular dependencies.
|
|
|
|
return config.theme.update(api, config);
|
|
|
|
}).then(function () {
|
|
|
|
return when.join(
|
|
|
|
// Check for or initialise a dbHash.
|
|
|
|
initDbHashAndFirstRun(),
|
|
|
|
// Initialize the permissions actions and objects
|
|
|
|
permissions.init()
|
|
|
|
);
|
|
|
|
}).then(function () {
|
2013-11-28 06:45:01 +04:00
|
|
|
return when.join(
|
|
|
|
// Initialise mail after first run,
|
|
|
|
// passing in config module to prevent
|
|
|
|
// circular dependencies.
|
2013-12-06 12:51:35 +04:00
|
|
|
mailer.init(),
|
2013-12-06 18:13:15 +04:00
|
|
|
helpers.loadCoreHelpers(config)
|
2013-11-28 06:45:01 +04:00
|
|
|
);
|
2013-11-20 06:43:55 +04:00
|
|
|
}).then(function () {
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ##Configuration
|
|
|
|
// set the view engine
|
|
|
|
server.set('view engine', 'hbs');
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-17 22:40:26 +04:00
|
|
|
// set the configured URL
|
2013-12-06 18:13:15 +04:00
|
|
|
server.set('ghost root', config.theme().path);
|
2013-11-17 22:40:26 +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-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')) {
|
|
|
|
return _.isString(config().server.socket) ? config().server.socket : path.join(__dirname, '../content/', 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,
|
|
|
|
"\nPlease go to http://nodejs.org to get the latest 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
|
|
|
|
);
|
|
|
|
fs.chmod(getSocket(), '0744');
|
|
|
|
});
|
|
|
|
|
|
|
|
} 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;
|