2013-05-16 15:21:13 +04:00
|
|
|
// # Ghost Data API
|
|
|
|
// Provides access to the data model
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
when = require('when'),
|
|
|
|
errors = require('../errorHandling'),
|
|
|
|
db = require('./db'),
|
|
|
|
settings = require('./settings'),
|
|
|
|
notifications = require('./notifications'),
|
|
|
|
dataProvider = require('../models'),
|
|
|
|
config = require('../config'),
|
|
|
|
posts = require('./posts'),
|
|
|
|
users = require('./users'),
|
|
|
|
tags = require('./tags'),
|
2013-06-25 15:43:15 +04:00
|
|
|
requestHandler,
|
2013-12-06 12:51:35 +04:00
|
|
|
init;
|
2013-08-30 15:20:30 +04:00
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// ## Request Handlers
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-09-24 19:21:43 +04:00
|
|
|
function invalidateCache(req, res, result) {
|
|
|
|
var parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
|
|
|
|
method = req.method,
|
2013-11-03 21:13:19 +04:00
|
|
|
endpoint = parsedUrl[4],
|
|
|
|
id = parsedUrl[5],
|
|
|
|
cacheInvalidate,
|
|
|
|
jsonResult = result.toJSON ? result.toJSON() : result;
|
|
|
|
|
2013-09-24 19:21:43 +04:00
|
|
|
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
|
|
|
|
if (endpoint === 'settings' || endpoint === 'users') {
|
|
|
|
cacheInvalidate = "/*";
|
|
|
|
} else if (endpoint === 'posts') {
|
|
|
|
cacheInvalidate = "/, /page/*, /rss/, /rss/*";
|
2013-11-03 21:13:19 +04:00
|
|
|
if (id && jsonResult.slug) {
|
|
|
|
cacheInvalidate += ', /' + jsonResult.slug + '/';
|
2013-09-24 19:21:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cacheInvalidate) {
|
|
|
|
res.set({
|
|
|
|
"X-Cache-Invalidate": cacheInvalidate
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// ### requestHandler
|
2013-06-25 15:43:15 +04:00
|
|
|
// decorator for api functions which are called via an HTTP request
|
|
|
|
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
|
|
|
|
requestHandler = function (apiMethod) {
|
|
|
|
return function (req, res) {
|
2013-08-09 05:22:49 +04:00
|
|
|
var options = _.extend(req.body, req.query, req.params),
|
|
|
|
apiContext = {
|
|
|
|
user: req.session && req.session.user
|
2013-11-28 22:25:45 +04:00
|
|
|
},
|
|
|
|
postRouteIndex,
|
|
|
|
i;
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
settings.read('permalinks').then(function (permalinks) {
|
|
|
|
// If permalinks have changed, find old post route
|
|
|
|
if (req.body.permalinks && req.body.permalinks !== permalinks) {
|
|
|
|
for (i = 0; i < req.app.routes.get.length; i += 1) {
|
|
|
|
if (req.app.routes.get[i].path === config.paths().webroot + permalinks) {
|
|
|
|
postRouteIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-28 22:25:45 +04:00
|
|
|
}
|
|
|
|
}
|
2013-08-09 05:22:49 +04:00
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
return apiMethod.call(apiContext, options).then(function (result) {
|
|
|
|
// Reload post route
|
|
|
|
if (postRouteIndex) {
|
|
|
|
req.app.get(permalinks, req.app.routes.get.splice(postRouteIndex, 1)[0].callbacks);
|
|
|
|
}
|
2013-11-28 22:25:45 +04:00
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
invalidateCache(req, res, result);
|
|
|
|
res.json(result || {});
|
|
|
|
}, function (error) {
|
|
|
|
var errorCode = error.errorCode || 500,
|
|
|
|
errorMsg = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};
|
|
|
|
res.json(errorCode, errorMsg);
|
|
|
|
});
|
2013-06-08 09:05:40 +04:00
|
|
|
});
|
|
|
|
};
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
init = function () {
|
|
|
|
return settings.updateSettingsCache();
|
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Public API
|
2013-12-06 12:51:35 +04:00
|
|
|
module.exports = {
|
|
|
|
posts: posts,
|
|
|
|
users: users,
|
|
|
|
tags: tags,
|
|
|
|
notifications: notifications,
|
|
|
|
settings: settings,
|
|
|
|
db: db,
|
|
|
|
requestHandler: requestHandler,
|
|
|
|
init: init
|
|
|
|
};
|