-
Notifications
You must be signed in to change notification settings - Fork 122
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
Conversation
componentInfo: new ComponentInfoRoute(repository), | ||
componentPreview: new ComponentPreviewRoute(repository), | ||
componentInfo: new ComponentInfoRoute(conf, repository), | ||
componentPreview: new ComponentPreviewRoute(conf, repository), |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as said there ^
There was a problem hiding this 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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as said there ^
b3afac9
to
b2e61e6
Compare
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense for me
There was a problem hiding this 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}); |
There was a problem hiding this comment.
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}); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local reference
There was a problem hiding this comment.
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.
d9fed88
to
c35ef82
Compare
c35ef82
to
5041069
Compare
@matteofigus this time should be good 💨 |
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
andcomponent-preview
are conceptually identical so I suggest reviewing fromcomponent-preview
file as it is little bit smaller, so more manageable.