Skip to content

Instantly share code, notes, and snippets.

@jkrems
Last active August 29, 2015 14:05
Show Gist options
  • Save jkrems/1ffe063e998dd349ac87 to your computer and use it in GitHub Desktop.
Save jkrems/1ffe063e998dd349ac87 to your computer and use it in GitHub Desktop.
gofer-blog
var entityId = 'xy';
var loadEntity = cached.deferred(_.partial(myService.fetch, '/entity/' + entityId));
cached('myService').getOrElse(entityId, loadEntity, function(err, entity) {
// 80% of time this works every time
});
// will be logged with serviceName=myService, endpointName=cats, methodName=index
myService.cats.index(cb);
// will be logged with serviceName=myService, endpointName=cats, methodName=put
myService.cats.save('kitty', { fur: 'fluffy' }, cb);
var goferFor = require('gofer');
var MyService = goferFor('myService'); // serviceName = myService
MyService.registerEndpoints({
// endpointName = cats
cats: function(request) {
return {
index: function(cb) {
return request({ uri: '/cats', methodName: 'index' }, cb);
},
show: function(id, cb) {
return request({ uri: '/cats/' + id, methodName: 'show' }, cb);
},
save: function(id, data, cb) {
return request({ uri: '/cats/' + id, json: data }, cb);
}
};
}
});
globalDefaults:
connectTimeout: 100
timeout: 2000
myApi:
baseUrl: "https://my-api"
qs:
client_id: "SOME_CLIENT_ID"
github:
baseUrl: "https://api.github.com/v3"
clientId: "XXX"
clientSecret: "XYZ"
var http = require('http');
var Hub = require('gofer/hub');
var MyService = require('my-service-gofer');
var OtherService = require('other-service-gofer');
var hub = new Hub();
hub.on('success', function() {});
hub.on('socketQueueing', function() {});
// ...
http.createServer(function(req, res) {
var requestId = req.headers['x-request-id'] || generateUUID();
var config = { globalDefaults: { requestId: requestId } };
var myService = new MyService(config, hub);
var otherService = new OtherService(config, hub);
myService.fetch('/some-url').pipe(res);
}).listen(process.env.PORT || 3000);
myApi.fetch({ uri: '/cats' });
request({
connectTimeout: 100, // from globalDefaults
timeout: 2000,
baseUrl: 'https://my-api', // from service defaults
qs: { client_id: 'SOME_CLIENT_ID' },
uri: '/cats' // explicit options
});
function addOAuth2Token(options) {
if (options.accessToken) {
options.headers = options.headers || {};
options.headers.authorization = 'token ' + options.accessToken;
delete options.accessToken;
}
return options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment