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-26 07:22:59 +04:00
|
|
|
Ghost = require('../ghost'),
|
|
|
|
helpers = require('./helpers'),
|
|
|
|
middleware = require('./middleware'),
|
|
|
|
routes = require('./routes'),
|
|
|
|
packageInfo = require('../../package.json'),
|
2013-09-06 19:54:50 +04:00
|
|
|
|
|
|
|
// Variables
|
2013-11-20 06:43:55 +04:00
|
|
|
ghost = new Ghost(),
|
2013-11-23 21:54:47 +04:00
|
|
|
setup,
|
|
|
|
init;
|
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-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) {
|
|
|
|
when(ghost.init()).then(function () {
|
2013-11-20 17:58:52 +04:00
|
|
|
return helpers.loadCoreHelpers(ghost, config);
|
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
|
|
|
|
server.set('ghost root', ghost.blogGlobals().path);
|
|
|
|
|
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
|
|
|
|
middleware(server);
|
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
|
|
|
// Expose the express server on the ghost instance.
|
|
|
|
ghost.server = server;
|
|
|
|
|
|
|
|
// Initialize plugins then start the server
|
|
|
|
plugins.init(ghost).then(function () {
|
|
|
|
|
|
|
|
// ## 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;
|