Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle fallbackRegistryUrl for ~info and ~preview #380

Merged
merged 10 commits into from
Mar 10, 2017

Conversation

pbazydlo
Copy link
Collaborator

@pbazydlo pbazydlo commented Feb 9, 2017

The aim of this PR is to fill in some of the missing pieces for fallbackRegistryUrl toggle.
The fallback functionality is added to ~info and ~preview endpoints.

It is probably best to read this PR in whitespace insensitive mode as some code is nested into one more callback (so it became more indented): https://github.com/opentable/oc/pull/380/files?w=1

Changes to component-info and component-preview are conceptually identical so I suggest reviewing from component-preview file as it is little bit smaller, so more manageable.

componentInfo: new ComponentInfoRoute(repository),
componentPreview: new ComponentPreviewRoute(repository),
componentInfo: new ComponentInfoRoute(conf, repository),
componentPreview: new ComponentPreviewRoute(conf, repository),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those two endpoints now need configuration for fallbackRegistryUrl


module.exports = function(repository){
return function(req, res){
function getParams(component) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've split this function from the main callback, because it seemed little bit hard to read.

This kind of refactor probably shouldn't be in this PR, so if it is prepared to revert this change I can make a commit for that.

if(err){
res.errorDetails = err;
return res.status(404).json({ err: err });
function getParsedAuthor(component) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've split this function from the main callback, because it seemed little bit hard to read.

This kind of refactor probably shouldn't be in this PR, so if it is prepared to revert this change I can make a commit for that.

return _.isString(author) ? parseAuthor(author) : author;
}

function addGetRepositoryUrlFunction(component) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've split this function from the main callback, because it seemed little bit hard to read.

This kind of refactor probably shouldn't be in this PR, so if it is prepared to revert this change I can make a commit for that.

var params = {},
author = component.author || {},
parsedAuthor = _.isString(author) ? parseAuthor(author) : author;
getComponentInfoOrPreviewFallback(conf, req, res, localRegistryError, localComponent, function(err, component) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the function that will try to do a fallback call to registry url

I've also looked briefly at replacing repository.getComponent with getcomponenthelper, however, helper returned very different format of response (that I would not be able to map).

return callback(res[0]);
});
},
getComponentInfoOrPreviewFallback: function (conf, req, res, localRegistryError, component, callback) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some trouble coming up with good names for this and the other (getComponent) functions.

The getComponent function goes to fallback repository and makes a POST query for a rendered component.

The getComponentInfoOrPreviewFallback function repeats a GET query to fallback registry and was tested only with ~info and ~preview endpoints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer if we keep it modularised to keep consistency, example get-component-info-fallback.js and get-component-fallback.js. Each thing exposes one thing with the same name.

return request({
method: 'get',
url: conf.fallbackRegistryUrl + path.substr(1, path.length - 1),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req.originalUrl comes with a / as first character and we already have trailing / in conf.fallbackRegistryUrl is it prefered if I used url library or is this fine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as said there ^

@pbazydlo pbazydlo changed the title [WIP] Handle fallbackRegistryUrl for ~info and ~preview Handle fallbackRegistryUrl for ~info and ~preview Feb 9, 2017
@matteofigus matteofigus mentioned this pull request Feb 14, 2017
Copy link
Member

@matteofigus matteofigus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can you review #382 first and then rebase this with that?

@@ -12,7 +12,7 @@ var Client = require('../../../../client');
var detective = require('../../domain/plugins-detective');
var eventsHandler = require('../../domain/events-handler');
var GetComponentRetrievingInfo = require('./get-component-retrieving-info');
var getComponentFallback = require('./get-component-fallback');
var getComponentFallback = require('./get-component-fallback').getComponent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit inconsistent. Can we just expose it as we do with all the other stuff?

return callback(res[0]);
});
},
getComponentInfoOrPreviewFallback: function (conf, req, res, localRegistryError, component, callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer if we keep it modularised to keep consistency, example get-component-info-fallback.js and get-component-fallback.js. Each thing exposes one thing with the same name.

var path = req.originalUrl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am not in love about using req.originalUrl. Can we build the url by accessing component variable and using domain/url-builder.js?

return request({
method: 'get',
url: conf.fallbackRegistryUrl + path.substr(1, path.length - 1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as said there ^

@pbazydlo pbazydlo force-pushed the improveFallbackRegistryForDev branch from b3afac9 to b2e61e6 Compare March 9, 2017 13:43
Copy link
Member

@matteofigus matteofigus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks really good. Only thing, the naming conflict I mentioned in my comment.

module.exports = function(conf, repository){
return function(req, res){
repository.getComponent(req.params.componentName, req.params.componentVersion, function(localRegistryError, localComponent){
if(localRegistryError && conf.fallbackRegistryUrl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One little thing here in regards of naming. In many other places in the code, we refer "local" registry as registry running as oc dev, as way to differentiate it from the "default" registry running as real instance.

I think what you are calling local here can be confused with the other "local" so I think I would prefer us to change the naming. What about making local => default (so registryError, component) and the other default => fallback (so fallbackError, fallbackComponent)?

Another idea is to call the "local" "current"? Or maybe you have other ideas?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense for me

Copy link
Member

@matteofigus matteofigus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol found more local references. I think I highlighted them all this time 🎉

});

if (fallbackErr) {
return callback({localError: localRegistryError, fallbackError: fallbackErr});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another local reference

try {
return callback(null, JSON.parse(fallbackResponse));
} catch (parseError) {
return callback({localError: localRegistryError, fallbackError: 'Could not parse fallback response: ' + fallbackResponse});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

});
},
getComponentPreview: function (conf, req, res, localRegistryError, callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

},
getComponentPreview: function (conf, req, res, localRegistryError, callback) {
getComponentFallbackForViewType(urlBuilder.componentPreview, conf, req, res, localRegistryError, callback);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

getComponentFallbackForViewType(urlBuilder.componentPreview, conf, req, res, localRegistryError, callback);
},
getComponentInfo: function (conf, req, res, localRegistryError, callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

},
getComponentInfo: function (conf, req, res, localRegistryError, callback) {
getComponentFallbackForViewType(urlBuilder.componentInfo, conf, req, res, localRegistryError, callback);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

var isHtmlRequest = !!req.headers.accept && req.headers.accept.indexOf('text/html') >= 0;
function componentInfo(err, req, res, component) {
if(err) {
res.errorDetails = err.localError;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

res.errorDetails = err;
function componentPreview(err, req, res, component) {
if(err) {
res.errorDetails = err.localError;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

var _ = require('underscore');

module.exports = function(fallbackRegistryUrl, headers, component, callback) {
function getComponentFallbackForViewType(buildUrl, conf, req, res, localRegistryError, callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local reference

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn, I should have checked.

@pbazydlo pbazydlo force-pushed the improveFallbackRegistryForDev branch from d9fed88 to c35ef82 Compare March 10, 2017 13:23
@pbazydlo pbazydlo force-pushed the improveFallbackRegistryForDev branch from c35ef82 to 5041069 Compare March 10, 2017 13:27
@pbazydlo
Copy link
Collaborator Author

@matteofigus this time should be good 💨

@matteofigus matteofigus merged commit 36167f0 into master Mar 10, 2017
@matteofigus matteofigus deleted the improveFallbackRegistryForDev branch March 10, 2017 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants