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.
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// Module dependencies
|
2013-11-20 06:43:55 +04:00
|
|
|
var configLoader = require('./config-loader.js'),
|
|
|
|
express = require('express'),
|
|
|
|
when = require('when'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
semver = require('semver'),
|
|
|
|
fs = require('fs'),
|
|
|
|
errors = require('./server/errorHandling'),
|
|
|
|
plugins = require('./server/plugins'),
|
|
|
|
path = require('path'),
|
|
|
|
Ghost = require('./ghost'),
|
|
|
|
helpers = require('./server/helpers'),
|
|
|
|
middleware = require('./server/middleware'),
|
|
|
|
routes = require('./server/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(),
|
|
|
|
init,
|
|
|
|
setup;
|
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"
|
|
|
|
// for help in seeing swallowed promise errors.
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
require('when/monitor/console');
|
|
|
|
}
|
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Initializes the ghost application.
|
|
|
|
function init(app) {
|
|
|
|
if (!app) {
|
|
|
|
app = express();
|
|
|
|
}
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
configLoader.loadConfig().then(function () {
|
|
|
|
// The server and its dependencies require a populated config
|
|
|
|
setup(app);
|
|
|
|
}).otherwise(errors.logAndThrowError);
|
|
|
|
}
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-09-18 07:35:08 +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) {
|
|
|
|
when(ghost.init()).then(function () {
|
|
|
|
return helpers.loadCoreHelpers(ghost);
|
|
|
|
}).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-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() {
|
|
|
|
if (ghost.config().server.hasOwnProperty('socket')) {
|
|
|
|
return _.isString(ghost.config().server.socket) ? ghost.config().server.socket : path.join(__dirname, '../content/', process.env.NODE_ENV + '.socket');
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
ghost.config().url,
|
|
|
|
"\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",
|
|
|
|
getSocket() || ghost.config().server.host + ':' + ghost.config().server.port,
|
|
|
|
"\nUrl configured as:",
|
|
|
|
ghost.config().url,
|
|
|
|
"\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 06:43:55 +04:00
|
|
|
ghost.config().server.port,
|
|
|
|
ghost.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
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = init;
|