2013-05-11 20:44:25 +04:00
|
|
|
// # Ghost main app file
|
|
|
|
|
2013-05-17 01:16:09 +04:00
|
|
|
/*global require, __dirname */
|
2013-05-11 20:44:25 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Module dependencies.
|
|
|
|
var express = require('express'),
|
2013-05-27 23:27:41 +04:00
|
|
|
when = require('when'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
errors = require('./core/shared/errorHandling'),
|
2013-05-11 20:44:25 +04:00
|
|
|
admin = require('./core/admin/controllers'),
|
|
|
|
frontend = require('./core/frontend/controllers'),
|
2013-05-16 15:21:13 +04:00
|
|
|
api = require('./core/shared/api'),
|
2013-05-11 20:44:25 +04:00
|
|
|
flash = require('connect-flash'),
|
|
|
|
Ghost = require('./core/ghost'),
|
|
|
|
I18n = require('./core/lang/i18n'),
|
2013-05-27 23:27:41 +04:00
|
|
|
filters = require('./core/frontend/filters'),
|
2013-05-19 02:03:57 +04:00
|
|
|
helpers = require('./core/frontend/helpers'),
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-16 23:22:01 +04:00
|
|
|
// ## Custom Middleware
|
2013-05-19 02:03:57 +04:00
|
|
|
auth,
|
2013-05-24 14:44:15 +04:00
|
|
|
authAPI,
|
2013-05-27 23:27:41 +04:00
|
|
|
ghostLocals,
|
2013-06-16 23:22:01 +04:00
|
|
|
|
|
|
|
// ## Variables
|
2013-05-27 23:27:41 +04:00
|
|
|
loading = when.defer(),
|
2013-05-19 02:03:57 +04:00
|
|
|
|
2013-05-17 01:16:09 +04:00
|
|
|
/**
|
|
|
|
* Create new Ghost object
|
|
|
|
* @type {Ghost}
|
|
|
|
*/
|
2013-06-16 23:22:01 +04:00
|
|
|
ghost = new Ghost();
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-05-29 04:10:39 +04:00
|
|
|
|
2013-05-11 20:44:25 +04:00
|
|
|
ghost.app().configure('development', function () {
|
|
|
|
ghost.app().use(express.favicon(__dirname + '/content/images/favicon.ico'));
|
2013-05-20 01:51:27 +04:00
|
|
|
ghost.app().use(express.errorHandler({ dumpExceptions: true, showStack: true }));
|
|
|
|
ghost.app().use(express.logger('dev'));
|
2013-05-11 20:44:25 +04:00
|
|
|
ghost.app().use(I18n.load(ghost));
|
|
|
|
ghost.app().use(express.bodyParser());
|
|
|
|
ghost.app().use(express.cookieParser('try-ghost'));
|
2013-05-24 10:19:19 +04:00
|
|
|
ghost.app().use(express.cookieSession({ cookie: { maxAge: 60000000 }}));
|
2013-05-11 20:44:25 +04:00
|
|
|
ghost.app().use(ghost.initTheme(ghost.app()));
|
2013-05-19 15:19:39 +04:00
|
|
|
ghost.app().use(flash());
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup login details
|
|
|
|
* p.s. love it.
|
|
|
|
*
|
|
|
|
* @type {*}
|
|
|
|
*/
|
2013-05-19 15:19:39 +04:00
|
|
|
auth = function (req, res, next) {
|
|
|
|
if (!req.session.user) {
|
|
|
|
req.flash('warn', "Please login");
|
|
|
|
res.redirect('/ghost/login/?redirect=' + encodeURIComponent(req.path));
|
2013-05-24 14:44:15 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
authAPI = function (req, res, next) {
|
|
|
|
if (!req.session.user) {
|
|
|
|
// TODO: standardize error format/codes/messages
|
|
|
|
var err = { code: 42, message: 'Please login' };
|
|
|
|
res.json(401, { error: err });
|
|
|
|
return;
|
2013-05-19 15:19:39 +04:00
|
|
|
}
|
2013-05-24 14:44:15 +04:00
|
|
|
next();
|
2013-05-19 15:19:39 +04:00
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
|
|
|
/**
|
2013-05-27 23:27:41 +04:00
|
|
|
* Expose the standard locals that every external page should have available;
|
2013-06-16 23:22:01 +04:00
|
|
|
* path, navItems and settingsCache
|
2013-05-11 20:44:25 +04:00
|
|
|
*/
|
2013-05-31 09:58:20 +04:00
|
|
|
ghostLocals = function (req, res, next) {
|
|
|
|
ghost.doFilter('ghostNavItems', {path: req.path, navItems: []}, function (navData) {
|
2013-05-27 23:27:41 +04:00
|
|
|
// Make sure we have a locals value.
|
|
|
|
res.locals = res.locals || {};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-16 23:22:01 +04:00
|
|
|
// Extend it with nav data and settings
|
2013-05-27 23:27:41 +04:00
|
|
|
_.extend(res.locals, navData, {
|
2013-06-16 23:22:01 +04:00
|
|
|
messages: req.flash(),
|
|
|
|
settings: ghost.settings(),
|
|
|
|
availableThemes: ghost.paths().availableThemes,
|
|
|
|
availablePlugins: ghost.paths().availablePlugins
|
2013-05-27 23:27:41 +04:00
|
|
|
});
|
2013-05-19 15:19:39 +04:00
|
|
|
|
2013-05-27 23:27:41 +04:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-05-27 23:27:41 +04:00
|
|
|
// Expose the promise we will resolve after our pre-loading
|
|
|
|
ghost.loaded = loading.promise;
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-05-30 18:01:29 +04:00
|
|
|
when.all([ghost.init(), filters.loadCoreFilters(ghost), helpers.loadCoreHelpers(ghost)]).then(function () {
|
2013-06-09 20:45:17 +04:00
|
|
|
|
2013-06-16 23:22:01 +04:00
|
|
|
// post init config
|
|
|
|
ghost.app().use(ghostLocals);
|
2013-06-09 20:45:17 +04:00
|
|
|
|
2013-05-27 23:27:41 +04:00
|
|
|
/**
|
|
|
|
* API routes..
|
|
|
|
* @todo auth should be public auth not user auth
|
|
|
|
*/
|
|
|
|
ghost.app().get('/api/v0.1/posts', authAPI, api.requestHandler(api.posts.browse));
|
|
|
|
ghost.app().post('/api/v0.1/posts', authAPI, api.requestHandler(api.posts.add));
|
|
|
|
ghost.app().get('/api/v0.1/posts/:id', authAPI, api.requestHandler(api.posts.read));
|
|
|
|
ghost.app().put('/api/v0.1/posts/:id', authAPI, api.requestHandler(api.posts.edit));
|
|
|
|
ghost.app().del('/api/v0.1/posts/:id', authAPI, api.requestHandler(api.posts.destroy));
|
2013-06-16 23:39:54 +04:00
|
|
|
ghost.app().get('/api/v0.1/settings', authAPI, api.cachedSettingsRequestHandler(api.settings.browse));
|
|
|
|
ghost.app().get('/api/v0.1/settings/:key', authAPI, api.cachedSettingsRequestHandler(api.settings.read));
|
|
|
|
ghost.app().put('/api/v0.1/settings', authAPI, api.cachedSettingsRequestHandler(api.settings.edit));
|
2013-05-27 23:27:41 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Admin routes..
|
|
|
|
* @todo put these somewhere in admin
|
|
|
|
*/
|
|
|
|
ghost.app().get(/^\/logout\/?$/, admin.logout);
|
|
|
|
ghost.app().get('/ghost/login/', admin.login);
|
2013-06-15 22:11:17 +04:00
|
|
|
ghost.app().get('/ghost/signup/', admin.signup);
|
2013-05-27 23:27:41 +04:00
|
|
|
ghost.app().post('/ghost/login/', admin.auth);
|
2013-06-15 22:11:17 +04:00
|
|
|
ghost.app().post('/ghost/signup/', admin.doRegister);
|
2013-05-27 23:27:41 +04:00
|
|
|
ghost.app().get('/ghost/editor/:id', auth, admin.editor);
|
|
|
|
ghost.app().get('/ghost/editor', auth, admin.editor);
|
2013-05-29 11:26:57 +04:00
|
|
|
ghost.app().get('/ghost/content', auth, admin.content);
|
2013-06-08 09:05:40 +04:00
|
|
|
ghost.app().get('/ghost/settings*', auth, admin.settings);
|
2013-05-27 23:27:41 +04:00
|
|
|
ghost.app().get('/ghost/debug', auth, admin.debug.index);
|
|
|
|
ghost.app().get('/ghost/debug/db/delete/', auth, admin.debug.dbdelete);
|
|
|
|
ghost.app().get('/ghost/debug/db/populate/', auth, admin.debug.dbpopulate);
|
|
|
|
ghost.app().get(/^\/(ghost$|(ghost-admin|admin|wp-admin|dashboard|login)\/?)/, auth, function (req, res) {
|
|
|
|
res.redirect('/ghost/');
|
|
|
|
});
|
|
|
|
ghost.app().get('/ghost/', auth, admin.index);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frontend routes..
|
|
|
|
* @todo dynamic routing, homepage generator, filters ETC ETC
|
|
|
|
*/
|
2013-06-16 23:22:01 +04:00
|
|
|
ghost.app().get('/:slug', frontend.single);
|
|
|
|
ghost.app().get('/', frontend.homepage);
|
2013-05-27 23:27:41 +04:00
|
|
|
|
|
|
|
ghost.app().listen(3333, function () {
|
|
|
|
console.log("Express server listening on port " + 3333);
|
|
|
|
|
|
|
|
// Let everyone know we have finished loading
|
|
|
|
loading.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
}, errors.logAndThrowError);
|
2013-06-15 22:11:17 +04:00
|
|
|
}());
|