2013-05-16 12:21:13 +01:00
|
|
|
// # Ghost Data API
|
|
|
|
// Provides access to the data model
|
|
|
|
|
2013-12-06 09:51:35 +01:00
|
|
|
var _ = require('underscore'),
|
|
|
|
when = require('when'),
|
2013-12-30 02:03:29 -05:00
|
|
|
config = require('../config'),
|
2013-12-06 09:51:35 +01:00
|
|
|
errors = require('../errorHandling'),
|
|
|
|
db = require('./db'),
|
|
|
|
settings = require('./settings'),
|
|
|
|
notifications = require('./notifications'),
|
|
|
|
posts = require('./posts'),
|
|
|
|
users = require('./users'),
|
|
|
|
tags = require('./tags'),
|
2013-06-25 12:43:15 +01:00
|
|
|
requestHandler,
|
2013-12-06 09:51:35 +01:00
|
|
|
init;
|
2013-08-30 13:20:30 +02:00
|
|
|
|
2013-08-06 20:27:56 +01:00
|
|
|
// ## Request Handlers
|
2013-06-25 12:43:15 +01:00
|
|
|
|
2013-09-24 17:21:43 +02:00
|
|
|
function invalidateCache(req, res, result) {
|
|
|
|
var parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
|
|
|
|
method = req.method,
|
2013-11-03 18:13:19 +01:00
|
|
|
endpoint = parsedUrl[4],
|
|
|
|
id = parsedUrl[5],
|
|
|
|
cacheInvalidate,
|
|
|
|
jsonResult = result.toJSON ? result.toJSON() : result;
|
|
|
|
|
2013-09-24 17:21:43 +02:00
|
|
|
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
|
2013-12-25 01:05:20 +01:00
|
|
|
if (endpoint === 'settings' || endpoint === 'users' || endpoint === 'db') {
|
2013-09-24 17:21:43 +02:00
|
|
|
cacheInvalidate = "/*";
|
|
|
|
} else if (endpoint === 'posts') {
|
|
|
|
cacheInvalidate = "/, /page/*, /rss/, /rss/*";
|
2013-11-03 18:13:19 +01:00
|
|
|
if (id && jsonResult.slug) {
|
|
|
|
cacheInvalidate += ', /' + jsonResult.slug + '/';
|
2013-09-24 17:21:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cacheInvalidate) {
|
|
|
|
res.set({
|
|
|
|
"X-Cache-Invalidate": cacheInvalidate
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 20:27:56 +01:00
|
|
|
// ### requestHandler
|
2013-06-25 12:43:15 +01: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 02:22:49 +01:00
|
|
|
var options = _.extend(req.body, req.query, req.params),
|
|
|
|
apiContext = {
|
|
|
|
user: req.session && req.session.user
|
2013-12-30 02:03:29 -05:00
|
|
|
};
|
2013-11-28 19:25:45 +01:00
|
|
|
|
2013-12-30 02:03:29 -05:00
|
|
|
return apiMethod.call(apiContext, options).then(function (result) {
|
|
|
|
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 02:05:40 -03:00
|
|
|
});
|
|
|
|
};
|
2013-06-25 12:43:15 +01:00
|
|
|
};
|
|
|
|
|
2013-12-06 09:51:35 +01:00
|
|
|
init = function () {
|
|
|
|
return settings.updateSettingsCache();
|
|
|
|
};
|
|
|
|
|
2013-08-06 20:27:56 +01:00
|
|
|
// Public API
|
2013-12-06 09:51:35 +01:00
|
|
|
module.exports = {
|
|
|
|
posts: posts,
|
|
|
|
users: users,
|
|
|
|
tags: tags,
|
|
|
|
notifications: notifications,
|
|
|
|
settings: settings,
|
|
|
|
db: db,
|
|
|
|
requestHandler: requestHandler,
|
|
|
|
init: init
|
|
|
|
};
|