2013-09-06 19:54:50 +04:00
|
|
|
// Module dependencies
|
2013-09-24 14:46:30 +04:00
|
|
|
var express = require('express'),
|
|
|
|
when = require('when'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
colors = require('colors'),
|
|
|
|
semver = require('semver'),
|
2013-10-09 20:07:43 +04:00
|
|
|
fs = require('fs'),
|
2013-09-24 14:46:30 +04:00
|
|
|
slashes = require('connect-slashes'),
|
|
|
|
errors = require('./server/errorHandling'),
|
|
|
|
admin = require('./server/controllers/admin'),
|
|
|
|
frontend = require('./server/controllers/frontend'),
|
|
|
|
api = require('./server/api'),
|
|
|
|
path = require('path'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
Ghost = require('./ghost'),
|
|
|
|
helpers = require('./server/helpers'),
|
2013-10-05 14:17:08 +04:00
|
|
|
middleware = require('./server/middleware'),
|
2013-09-06 19:54:50 +04:00
|
|
|
packageInfo = require('../package.json'),
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
loading = when.defer(),
|
|
|
|
server = express(),
|
|
|
|
ghost = new Ghost();
|
|
|
|
|
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-09-06 19:54:50 +04:00
|
|
|
// ##Custom Middleware
|
|
|
|
|
|
|
|
// ### Auth Middleware
|
|
|
|
// Authenticate a request by redirecting to login if not logged in.
|
|
|
|
// We strip /ghost/ out of the redirect parameter for neatness
|
|
|
|
function auth(req, res, next) {
|
|
|
|
if (!req.session.user) {
|
|
|
|
var path = req.path.replace(/^\/ghost\/?/gi, ''),
|
|
|
|
redirect = '',
|
|
|
|
msg;
|
|
|
|
|
|
|
|
if (path !== '') {
|
|
|
|
msg = {
|
|
|
|
type: 'error',
|
|
|
|
message: 'Please Sign In',
|
|
|
|
status: 'passive',
|
|
|
|
id: 'failedauth'
|
|
|
|
};
|
|
|
|
// let's only add the notification once
|
|
|
|
if (!_.contains(_.pluck(ghost.notifications, 'id'), 'failedauth')) {
|
|
|
|
ghost.notifications.push(msg);
|
|
|
|
}
|
|
|
|
redirect = '?r=' + encodeURIComponent(path);
|
|
|
|
}
|
|
|
|
return res.redirect('/ghost/signin/' + redirect);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if we're logged in, and if so, redirect people back to dashboard
|
|
|
|
// Login and signup forms in particular
|
|
|
|
function redirectToDashboard(req, res, next) {
|
|
|
|
if (req.session.user) {
|
|
|
|
return res.redirect('/ghost/');
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
function redirectToSignup(req, res, next) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-06 19:54:50 +04:00
|
|
|
api.users.browse().then(function (users) {
|
|
|
|
if (users.length === 0) {
|
|
|
|
return res.redirect('/ghost/signup/');
|
|
|
|
}
|
2013-10-16 12:10:09 +04:00
|
|
|
next();
|
|
|
|
}).otherwise(function (err) {
|
|
|
|
return next(new Error(err));
|
2013-09-06 19:54:50 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// While we're here, let's clean up on aisle 5
|
|
|
|
// That being ghost.notifications, and let's remove the passives from there
|
|
|
|
// plus the local messages, as they have already been added at this point
|
|
|
|
// otherwise they'd appear one too many times
|
|
|
|
function cleanNotifications(req, res, next) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-06 19:54:50 +04:00
|
|
|
ghost.notifications = _.reject(ghost.notifications, function (notification) {
|
|
|
|
return notification.status === 'passive';
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ## AuthApi Middleware
|
|
|
|
// Authenticate a request to the API by responding with a 401 and json error details
|
|
|
|
function authAPI(req, res, next) {
|
|
|
|
if (!req.session.user) {
|
|
|
|
// TODO: standardize error format/codes/messages
|
|
|
|
res.json(401, { error: 'Please sign in' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ### GhostLocals Middleware
|
|
|
|
// Expose the standard locals that every external page should have available,
|
2013-09-17 00:40:53 +04:00
|
|
|
// separating between the theme and the admin
|
2013-09-06 19:54:50 +04:00
|
|
|
function ghostLocals(req, res, next) {
|
|
|
|
// Make sure we have a locals value.
|
|
|
|
res.locals = res.locals || {};
|
|
|
|
res.locals.version = packageInfo.version;
|
2013-09-18 01:11:02 +04:00
|
|
|
res.locals.path = req.path;
|
2013-10-22 11:33:30 +04:00
|
|
|
res.locals.csrfToken = req.csrfToken();
|
2013-09-06 19:54:50 +04:00
|
|
|
|
|
|
|
if (res.isAdmin) {
|
|
|
|
api.users.read({id: req.session.user}).then(function (currentUser) {
|
|
|
|
_.extend(res.locals, {
|
|
|
|
currentUser: {
|
2013-10-18 00:38:49 +04:00
|
|
|
name: currentUser.name,
|
|
|
|
email: currentUser.email,
|
|
|
|
image: currentUser.image
|
2013-10-24 01:01:31 +04:00
|
|
|
},
|
|
|
|
messages: ghost.notifications
|
2013-09-06 19:54:50 +04:00
|
|
|
});
|
|
|
|
next();
|
|
|
|
}).otherwise(function () {
|
2013-10-24 01:01:31 +04:00
|
|
|
// Only show passive notifications
|
|
|
|
_.extend(res.locals, {
|
|
|
|
messages: _.reject(ghost.notifications, function (notification) {
|
|
|
|
return notification.status !== 'passive';
|
|
|
|
})
|
|
|
|
});
|
2013-09-06 19:54:50 +04:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ### DisableCachedResult Middleware
|
|
|
|
// Disable any caching until it can be done properly
|
|
|
|
function disableCachedResult(req, res, next) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-06 19:54:50 +04:00
|
|
|
res.set({
|
2013-09-24 14:46:30 +04:00
|
|
|
'Cache-Control': 'no-cache, must-revalidate',
|
|
|
|
'Expires': 'Sat, 26 Jul 1997 05:00:00 GMT'
|
2013-09-06 19:54:50 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2013-09-17 04:40:59 +04:00
|
|
|
// ### whenEnabled Middleware
|
|
|
|
// Selectively use middleware
|
|
|
|
// From https://github.com/senchalabs/connect/issues/676#issuecomment-9569658
|
|
|
|
function whenEnabled(setting, fn) {
|
|
|
|
return function settingEnabled(req, res, next) {
|
|
|
|
if (server.enabled(setting)) {
|
|
|
|
fn(req, res, next);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ### InitViews Middleware
|
|
|
|
// Initialise Theme or Admin Views
|
|
|
|
function initViews(req, res, next) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-17 04:40:59 +04:00
|
|
|
var hbsOptions;
|
|
|
|
|
|
|
|
if (!res.isAdmin) {
|
|
|
|
// self.globals is a hack til we have a better way of getting combined settings & config
|
|
|
|
hbsOptions = {templateOptions: {data: {blog: ghost.blogGlobals()}}};
|
|
|
|
|
|
|
|
if (ghost.themeDirectories[ghost.settings('activeTheme')].hasOwnProperty('partials')) {
|
|
|
|
// Check that the theme has a partials directory before trying to use it
|
|
|
|
hbsOptions.partialsDir = path.join(ghost.paths().activeTheme, 'partials');
|
|
|
|
}
|
|
|
|
|
|
|
|
server.engine('hbs', hbs.express3(hbsOptions));
|
|
|
|
server.set('views', ghost.paths().activeTheme);
|
|
|
|
} else {
|
|
|
|
server.engine('hbs', hbs.express3({partialsDir: ghost.paths().adminViews + 'partials'}));
|
|
|
|
server.set('views', ghost.paths().adminViews);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ### Activate Theme
|
|
|
|
// Helper for manageAdminAndTheme
|
|
|
|
function activateTheme() {
|
2013-10-31 22:02:34 +04:00
|
|
|
var stackLocation = _.indexOf(server.stack, _.find(server.stack, function (stackItem) {
|
2013-09-17 04:40:59 +04:00
|
|
|
return stackItem.route === '' && stackItem.handle.name === 'settingEnabled';
|
|
|
|
}));
|
2013-09-17 05:26:28 +04:00
|
|
|
|
|
|
|
// clear the view cache
|
|
|
|
server.cache = {};
|
2013-09-17 04:40:59 +04:00
|
|
|
server.disable(server.get('activeTheme'));
|
|
|
|
server.set('activeTheme', ghost.settings('activeTheme'));
|
|
|
|
server.enable(server.get('activeTheme'));
|
|
|
|
if (stackLocation) {
|
2013-10-05 14:17:08 +04:00
|
|
|
server.stack[stackLocation].handle = whenEnabled(server.get('activeTheme'), middleware.staticTheme(ghost));
|
2013-09-17 04:40:59 +04:00
|
|
|
}
|
2013-10-21 20:59:37 +04:00
|
|
|
|
|
|
|
// Update user error template
|
|
|
|
errors.updateActiveTheme(ghost.settings('activeTheme'));
|
2013-09-17 04:40:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ### ManageAdminAndTheme Middleware
|
|
|
|
// Uses the URL to detect whether this response should be an admin response
|
|
|
|
// This is used to ensure the right content is served, and is not for security purposes
|
|
|
|
function manageAdminAndTheme(req, res, next) {
|
|
|
|
// TODO improve this regex
|
|
|
|
res.isAdmin = /(^\/ghost\/)/.test(req.url);
|
|
|
|
if (res.isAdmin) {
|
|
|
|
server.enable('admin');
|
|
|
|
server.disable(server.get('activeTheme'));
|
|
|
|
} else {
|
|
|
|
server.enable(server.get('activeTheme'));
|
|
|
|
server.disable('admin');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the theme changed
|
|
|
|
if (ghost.settings('activeTheme') !== server.get('activeTheme')) {
|
|
|
|
// Change theme
|
|
|
|
if (!ghost.themeDirectories.hasOwnProperty(ghost.settings('activeTheme'))) {
|
|
|
|
if (!res.isAdmin) {
|
|
|
|
// Throw an error if the theme is not available, but not on the admin UI
|
|
|
|
errors.logAndThrowError('The currently active theme ' + ghost.settings('activeTheme') + ' is missing.');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
activateTheme();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// Expose the promise we will resolve after our pre-loading
|
|
|
|
ghost.loaded = loading.promise;
|
|
|
|
|
2013-09-27 00:30:45 +04:00
|
|
|
when(ghost.init()).then(function () {
|
|
|
|
return helpers.loadCoreHelpers(ghost);
|
|
|
|
}).then(function () {
|
2013-09-06 19:54:50 +04:00
|
|
|
|
|
|
|
// ##Configuration
|
2013-09-17 04:40:59 +04:00
|
|
|
var oneYear = 31536000000;
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-09-17 04:40:59 +04:00
|
|
|
// Logging configuration
|
2013-09-24 14:46:30 +04:00
|
|
|
if (server.get('env') !== 'development') {
|
2013-09-17 04:40:59 +04:00
|
|
|
server.use(express.logger());
|
|
|
|
} else {
|
2013-09-06 19:54:50 +04:00
|
|
|
server.use(express.logger('dev'));
|
2013-09-17 04:40:59 +04:00
|
|
|
}
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-09-18 07:35:08 +04:00
|
|
|
// Favicon
|
|
|
|
server.use(express.favicon(__dirname + '/shared/favicon.ico'));
|
|
|
|
|
2013-09-17 04:40:59 +04:00
|
|
|
// return the correct mime type for woff filess
|
|
|
|
express['static'].mime.define({'application/font-woff': ['woff']});
|
|
|
|
// Shared static config
|
|
|
|
server.use('/shared', express['static'](path.join(__dirname, '/shared')));
|
|
|
|
server.use('/content/images', express['static'](path.join(__dirname, '/../content/images')));
|
|
|
|
// Serve our built scripts; can't use /scripts here because themes already are
|
2013-09-24 14:46:30 +04:00
|
|
|
server.use('/built/scripts', express['static'](path.join(__dirname, '/built/scripts'), {
|
2013-09-17 04:40:59 +04:00
|
|
|
// Put a maxAge of one year on built scripts
|
|
|
|
maxAge: oneYear
|
|
|
|
}));
|
|
|
|
|
|
|
|
// First determine whether we're serving admin or theme content
|
|
|
|
server.use(manageAdminAndTheme);
|
|
|
|
|
|
|
|
// Admin only config
|
|
|
|
server.use('/ghost', whenEnabled('admin', express['static'](path.join(__dirname, '/client/assets'))));
|
|
|
|
|
|
|
|
// Theme only config
|
2013-10-05 14:17:08 +04:00
|
|
|
server.use(whenEnabled(server.get('activeTheme'), middleware.staticTheme(ghost)));
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-09-17 05:16:15 +04:00
|
|
|
// Add in all trailing slashes
|
|
|
|
server.use(slashes());
|
|
|
|
|
2013-09-19 03:30:31 +04:00
|
|
|
server.use(express.json());
|
|
|
|
server.use(express.urlencoded());
|
|
|
|
server.use('/ghost/upload/', express.multipart());
|
|
|
|
server.use('/ghost/upload/', express.multipart({uploadDir: __dirname + '/content/images'}));
|
2013-10-24 16:50:17 +04:00
|
|
|
server.use('/ghost/api/v0.1/db/', express.multipart());
|
2013-09-17 04:40:59 +04:00
|
|
|
server.use(express.cookieParser(ghost.dbHash));
|
2013-10-18 15:24:01 +04:00
|
|
|
server.use(express.cookieSession({ cookie : { maxAge: 12 * 60 * 60 * 1000 }}));
|
|
|
|
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-10-17 17:28:28 +04:00
|
|
|
//enable express csrf protection
|
|
|
|
server.use(express.csrf());
|
2013-09-17 04:40:59 +04:00
|
|
|
// local data
|
2013-09-06 19:54:50 +04:00
|
|
|
server.use(ghostLocals);
|
|
|
|
// So on every request we actually clean out reduntant passive notifications from the server side
|
|
|
|
server.use(cleanNotifications);
|
|
|
|
|
2013-09-17 05:16:15 +04:00
|
|
|
// set the view engine
|
|
|
|
server.set('view engine', 'hbs');
|
|
|
|
|
2013-09-17 04:40:59 +04:00
|
|
|
// Initialise the views
|
|
|
|
server.use(initViews);
|
|
|
|
|
|
|
|
// process the application routes
|
|
|
|
server.use(server.router);
|
|
|
|
|
|
|
|
// ### Error handling
|
2013-09-17 14:56:05 +04:00
|
|
|
// 404 Handler
|
2013-10-18 01:49:14 +04:00
|
|
|
server.use(errors.error404);
|
2013-09-17 14:56:05 +04:00
|
|
|
|
2013-09-19 06:50:06 +04:00
|
|
|
// 500 Handler
|
2013-10-18 01:49:14 +04:00
|
|
|
server.use(errors.error500);
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// ## Routing
|
|
|
|
|
|
|
|
// ### API routes
|
|
|
|
/* TODO: auth should be public auth not user auth */
|
|
|
|
// #### Posts
|
2013-10-24 16:50:17 +04:00
|
|
|
server.get('/ghost/api/v0.1/posts', authAPI, disableCachedResult, api.requestHandler(api.posts.browse));
|
|
|
|
server.post('/ghost/api/v0.1/posts', authAPI, disableCachedResult, api.requestHandler(api.posts.add));
|
|
|
|
server.get('/ghost/api/v0.1/posts/:id', authAPI, disableCachedResult, api.requestHandler(api.posts.read));
|
|
|
|
server.put('/ghost/api/v0.1/posts/:id', authAPI, disableCachedResult, api.requestHandler(api.posts.edit));
|
|
|
|
server.del('/ghost/api/v0.1/posts/:id', authAPI, disableCachedResult, api.requestHandler(api.posts.destroy));
|
2013-09-06 19:54:50 +04:00
|
|
|
// #### Settings
|
2013-10-24 16:50:17 +04:00
|
|
|
server.get('/ghost/api/v0.1/settings/', authAPI, disableCachedResult, api.requestHandler(api.settings.browse));
|
|
|
|
server.get('/ghost/api/v0.1/settings/:key/', authAPI, disableCachedResult, api.requestHandler(api.settings.read));
|
|
|
|
server.put('/ghost/api/v0.1/settings/', authAPI, disableCachedResult, api.requestHandler(api.settings.edit));
|
2013-09-06 19:54:50 +04:00
|
|
|
// #### Users
|
2013-10-24 16:50:17 +04:00
|
|
|
server.get('/ghost/api/v0.1/users/', authAPI, disableCachedResult, api.requestHandler(api.users.browse));
|
|
|
|
server.get('/ghost/api/v0.1/users/:id/', authAPI, disableCachedResult, api.requestHandler(api.users.read));
|
|
|
|
server.put('/ghost/api/v0.1/users/:id/', authAPI, disableCachedResult, api.requestHandler(api.users.edit));
|
2013-09-06 19:54:50 +04:00
|
|
|
// #### Tags
|
2013-10-24 16:50:17 +04:00
|
|
|
server.get('/ghost/api/v0.1/tags/', authAPI, disableCachedResult, api.requestHandler(api.tags.all));
|
2013-09-06 19:54:50 +04:00
|
|
|
// #### Notifications
|
2013-10-24 16:50:17 +04:00
|
|
|
server.del('/ghost/api/v0.1/notifications/:id', authAPI, disableCachedResult, api.requestHandler(api.notifications.destroy));
|
|
|
|
server.post('/ghost/api/v0.1/notifications/', authAPI, disableCachedResult, api.requestHandler(api.notifications.add));
|
2013-10-09 12:52:58 +04:00
|
|
|
// #### Import/Export
|
2013-10-24 16:50:17 +04:00
|
|
|
server.get('/ghost/api/v0.1/db/', auth, api.db['export']);
|
|
|
|
server.post('/ghost/api/v0.1/db/', auth, api.db['import']);
|
2013-09-06 19:54:50 +04:00
|
|
|
|
|
|
|
// ### Admin routes
|
|
|
|
/* TODO: put these somewhere in admin */
|
|
|
|
server.get(/^\/logout\/?$/, function redirect(req, res) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-06 19:54:50 +04:00
|
|
|
res.redirect(301, '/signout/');
|
|
|
|
});
|
|
|
|
server.get(/^\/signout\/?$/, admin.logout);
|
|
|
|
server.get('/ghost/login/', function redirect(req, res) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-06 19:54:50 +04:00
|
|
|
res.redirect(301, '/ghost/signin/');
|
|
|
|
});
|
|
|
|
server.get('/ghost/signin/', redirectToSignup, redirectToDashboard, admin.login);
|
|
|
|
server.get('/ghost/signup/', redirectToDashboard, admin.signup);
|
|
|
|
server.get('/ghost/forgotten/', redirectToDashboard, admin.forgotten);
|
|
|
|
server.post('/ghost/forgotten/', admin.resetPassword);
|
|
|
|
server.post('/ghost/signin/', admin.auth);
|
|
|
|
server.post('/ghost/signup/', admin.doRegister);
|
|
|
|
server.post('/ghost/changepw/', auth, admin.changepw);
|
2013-09-15 15:13:06 +04:00
|
|
|
server.get('/ghost/editor(/:id)/', auth, admin.editor);
|
|
|
|
server.get('/ghost/editor/', auth, admin.editor);
|
|
|
|
server.get('/ghost/content/', auth, admin.content);
|
2013-09-06 19:54:50 +04:00
|
|
|
server.get('/ghost/settings*', auth, admin.settings);
|
|
|
|
server.get('/ghost/debug/', auth, admin.debug.index);
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-09-19 03:30:31 +04:00
|
|
|
// We don't want to register bodyParser globally b/c of security concerns, so use multipart only here
|
2013-10-17 17:28:28 +04:00
|
|
|
server.post('/ghost/upload/', auth, admin.uploader);
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2013-09-23 01:31:21 +04:00
|
|
|
// redirect to /ghost and let that do the authentication to prevent redirects to /ghost//admin etc.
|
|
|
|
server.get(/^\/((ghost-admin|admin|wp-admin|dashboard|signin)\/?)/, function (req, res) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-23 01:31:21 +04:00
|
|
|
res.redirect('/ghost/');
|
|
|
|
});
|
|
|
|
server.get(/^\/(ghost$\/?)/, auth, function (req, res) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-06 19:54:50 +04:00
|
|
|
res.redirect('/ghost/');
|
|
|
|
});
|
|
|
|
server.get('/ghost/', redirectToSignup, auth, admin.index);
|
|
|
|
|
|
|
|
// ### Frontend routes
|
|
|
|
/* TODO: dynamic routing, homepage generator, filters ETC ETC */
|
|
|
|
server.get('/rss/', frontend.rss);
|
|
|
|
server.get('/rss/:page/', frontend.rss);
|
2013-09-17 04:40:59 +04:00
|
|
|
server.get('/page/:page/', frontend.homepage);
|
2013-09-17 05:52:29 +04:00
|
|
|
server.get('/:slug/', frontend.single);
|
2013-09-06 19:54:50 +04:00
|
|
|
server.get('/', frontend.homepage);
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-10-09 20:07:43 +04:00
|
|
|
// 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-09-06 19:54:50 +04:00
|
|
|
|
2013-10-09 20:07:43 +04:00
|
|
|
function startGhost() {
|
|
|
|
// Tell users if their node version is not supported, and exit
|
|
|
|
if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) {
|
|
|
|
console.log(
|
|
|
|
"\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
|
|
|
|
);
|
|
|
|
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-10-09 20:07:43 +04:00
|
|
|
// Startup & Shutdown messages
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(
|
|
|
|
"Ghost is running...".green,
|
|
|
|
"\nYour blog is now available on",
|
|
|
|
ghost.config().url,
|
|
|
|
"\nCtrl+C to shut down".grey
|
|
|
|
);
|
|
|
|
|
|
|
|
// ensure that Ghost exits correctly on Ctrl+C
|
|
|
|
process.on('SIGINT', function () {
|
2013-09-06 19:54:50 +04:00
|
|
|
console.log(
|
2013-10-09 20:07:43 +04:00
|
|
|
"\nGhost has shut down".red,
|
|
|
|
"\nYour blog is now offline"
|
2013-09-06 19:54:50 +04:00
|
|
|
);
|
2013-09-15 15:11:47 +04:00
|
|
|
process.exit(0);
|
2013-10-09 20:07:43 +04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log(
|
2013-11-03 18:12:09 +04:00
|
|
|
("Ghost is running in " + process.env.NODE_ENV + "...").green,
|
2013-10-09 20:07:43 +04:00
|
|
|
"\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 () {
|
2013-09-15 15:11:47 +04:00
|
|
|
console.log(
|
2013-10-09 20:07:43 +04:00
|
|
|
"\nGhost has shutdown".red,
|
|
|
|
"\nGhost was running for",
|
|
|
|
Math.round(process.uptime()),
|
|
|
|
"seconds"
|
2013-09-15 15:11:47 +04:00
|
|
|
);
|
2013-10-09 20:07:43 +04:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
2013-09-15 15:11:47 +04:00
|
|
|
|
2013-10-09 20:07:43 +04:00
|
|
|
// Let everyone know we have finished loading
|
|
|
|
loading.resolve();
|
|
|
|
}
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
// Expose the express server on the ghost instance.
|
|
|
|
ghost.server = server;
|
|
|
|
|
|
|
|
// Initialize plugins then start the server
|
|
|
|
ghost.initPlugins().then(function () {
|
|
|
|
|
|
|
|
// ## Start Ghost App
|
|
|
|
if (getSocket()) {
|
|
|
|
// Make sure the socket is gone before trying to create another
|
|
|
|
fs.unlink(getSocket(), function (err) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-09-15 01:34:12 +04:00
|
|
|
server.listen(
|
|
|
|
getSocket(),
|
|
|
|
startGhost
|
|
|
|
);
|
|
|
|
fs.chmod(getSocket(), '0744');
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
2013-10-09 20:07:43 +04:00
|
|
|
server.listen(
|
2013-09-15 01:34:12 +04:00
|
|
|
ghost.config().server.port,
|
|
|
|
ghost.config().server.host,
|
2013-10-09 20:07:43 +04:00
|
|
|
startGhost
|
|
|
|
);
|
2013-09-15 01:34:12 +04:00
|
|
|
}
|
2013-10-09 20:07:43 +04:00
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
});
|
2013-10-29 15:24:58 +04:00
|
|
|
}).otherwise(errors.logAndThrowError);
|