0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

issue #165 - reloading settings

- ghost.js - split the settings loading out of ghost.init, so that we have a function for loading / reloading settings
- api.js - implemented a new requestHandler, the cachedSettingsRequestHandler which handles all aspects of local caching for settings when making requests
- app.js - updated the settings api routes to use the new cached request handler
This commit is contained in:
Hannah Wolfe 2013-06-16 20:39:54 +01:00
parent 32bbf2ba57
commit 50eb91fe51
4 changed files with 68 additions and 24 deletions

6
app.js
View file

@ -108,9 +108,9 @@
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));
ghost.app().get('/api/v0.1/settings', authAPI, api.requestHandler(api.settings.browse));
ghost.app().get('/api/v0.1/settings/:key', authAPI, api.requestHandler(api.settings.read));
ghost.app().put('/api/v0.1/settings', authAPI, api.requestHandler(api.settings.edit));
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));
/**
* Admin routes..

View file

@ -87,8 +87,6 @@
// load Plugins...
// var f = new FancyFirstChar(ghost).init();
_.extend(instance, {
app: function () { return app; },
config: function () { return config; },
@ -125,6 +123,19 @@
var self = this;
return when.join(instance.dataProvider.init(), instance.getPaths()).then(function () {
return self.updateSettingsCache();
}, errors.logAndThrowError);
};
Ghost.prototype.updateSettingsCache = function (settings) {
var self = this;
settings = settings || {};
if (!_.isEmpty(settings)) {
self.settingsCache = settings;
} else {
// TODO: this should use api.browse
return models.Settings.findAll().then(function (result) {
var settings = {};
@ -136,7 +147,7 @@
self.settingsCache = settings;
}, errors.logAndThrowError);
}, errors.logAndThrowError);
}
};
/**

View file

@ -11,6 +11,8 @@
var Ghost = require('../ghost'),
_ = require('underscore'),
when = require('when'),
errors = require('./errorHandling'),
ghost = new Ghost(),
dataProvider = ghost.dataProvider,
@ -18,6 +20,7 @@
users,
settings,
requestHandler,
cachedSettingsRequestHandler,
settingsObject,
settingsCollection;
@ -25,37 +28,37 @@
posts = {
// takes filter / pagination parameters
// returns a page of posts in a json response
browse: function (options) {
browse: function browse(options) {
return dataProvider.Post.findPage(options);
},
// takes an identifier (id or slug?)
// returns a single post in a json response
read: function (args) {
read: function read(args) {
return dataProvider.Post.findOne(args);
},
// takes a json object with all the properties which should be updated
// returns the resulting post in a json response
edit: function (postData) {
edit: function edit(postData) {
return dataProvider.Post.edit(postData);
},
// takes a json object representing a post,
// returns the resulting post in a json response
add: function (postData) {
add: function add(postData) {
return dataProvider.Post.add(postData);
},
// takes an identifier (id or slug?)
// returns a json response with the id of the deleted post
destroy: function (args) {
destroy: function destroy(args) {
return dataProvider.Post.destroy(args.id);
}
};
// # Users
users = {
add: function (postData) {
add: function add(postData) {
return dataProvider.User.add(postData);
},
check: function (postData) {
check: function check(postData) {
return dataProvider.User.check(postData);
}
};
@ -78,21 +81,17 @@
};
settings = {
browse: function (options) {
browse: function browse(options) {
return dataProvider.Settings.browse(options).then(settingsObject);
},
read: function (options) {
read: function read(options) {
return dataProvider.Settings.read(options.key).then(function (setting) {
return _.pick(setting.toJSON(), 'key', 'value');
});
},
edit: function (settings) {
edit: function edit(settings) {
settings = settingsCollection(settings);
return dataProvider.Settings.edit(settings).then(settingsObject);
},
add: function (settings) {
settings = settingsCollection(settings);
return dataProvider.Settings.add(settings).then(settingsObject);
}
};
@ -114,8 +113,42 @@
};
};
cachedSettingsRequestHandler = function (apiMethod) {
if (!ghost.settings()) {
return requestHandler(apiMethod);
}
return function (req, res) {
var options = _.extend(req.body, req.query, req.params),
promise;
switch (apiMethod.name) {
case 'browse':
promise = when(ghost.settings());
break;
case 'read':
promise = when(ghost.settings()[options.key]);
break;
case 'edit':
promise = apiMethod(options).then(function (result) {
ghost.updateSettingsCache(result);
return result;
});
break;
default:
errors.logAndThrowError(new Error('Unknown method name for settings API: ' + apiMethod.name));
}
return promise.then(function (result) {
res.json(result || {});
}, function (error) {
res.json(400, {error: error});
});
};
};
module.exports.posts = posts;
module.exports.users = users;
module.exports.settings = settings;
module.exports.requestHandler = requestHandler;
module.exports.cachedSettingsRequestHandler = cachedSettingsRequestHandler;
}());

View file

@ -27,19 +27,19 @@
}
},
dataProviderInitSpy = sinon.spy(fakeDataProvider, "init"),
oldDataProvder = ghost.dataProvider;
oldDataProvider = ghost.dataProvider;
ghost.dataProvider = fakeDataProvider;
should.not.exist(ghost.globals());
should.not.exist(ghost.settings());
ghost.init().then(function () {
should.exist(ghost.globals());
should.exist(ghost.settings());
dataProviderInitSpy.called.should.equal(true);
ghost.dataProvider = oldDataProvder;
ghost.dataProvider = oldDataProvider;
done();
}).then(null, done);