Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
feat: support serving under a path prefix #120
feat: support serving under a path prefix #120
Changes from 4 commits
bfcb998
6906ef9
55b2b2a
aef0101
be02898
566858e
5ef2dd3
41818fc
e5a2611
454e91d
b593a82
7d6acb7
3a796c7
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Every config option may be set by CLI arguments and by environment variables. I think we need to add support for setting the prefix via a
PREFIX
env var.We also need to update this section of the README to note the new option:
https://github.com/mccutchen/go-httpbin/blob/main/README.md#configuration
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 implemented this. Now also with check for slash at beginning and end. But will be checked only on command line as the httpbin itself does not config checking.
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.
Given how often this pattern now shows up across handlers, I propose that we wrap the logic up in a helper method:
(That name would clash with the existing
doRedirect()
helper, which is a bit higher level and serves only theRedirect
/AbsoluteRedirect
/RelativeRedirect
handler. I'd suggest we rename the existingdoRedirect
to something likehandleRedirect
instead.)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.
Do you mean a
doRedirectCookies
method orcookies
a parameter to some method?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 mean there are now a bunch of places where we've got code like this, where we're constructing a redirect URL and must remember to include the prefix, so I'd like to have a helper method like the one I showed above.
This code would then become something like
and the new
doRedirect()
helper would take care of handling the prefix.Where it gets messy is that there's already an existing, higher level helper method
doRedirect()
helper that will need to be renamed. I think renaming the existing helper tohandleRedirect()
makes sense, as that better captures what the existing helper is actually doing.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 renamed the
doRedirect
tohandleRedirect
and used newdoRedirect
at two places. Is there a reason why https://github.com/waschik/go-httpbin/blob/7e81a9365c6fe9f1a4691742792dcd2e3d8a1fa6/httpbin/handlers.go#L883 is usingr.URL.String()
and notr.URL.Path
? Ifr.URL.Path
would be okay it would be possible to replace it also with the newdoRedirect
. Forr.URL.String()
I am not sure if it will not sometimes include the schema and then prefix might be wrong.For the absolute redirects new
doRedirect
would also not possible otherwise I get locations like/a-prefixhttp://...
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 this is to ensure that any query params are carried through the redirects, but it looks like we don't have any explicit test cases covering that behavior. Let's keep it as-is for now.
Ah that's a good point. Maybe he new
doRedirect()
should change its behavior based on whether the given argument starts withhttp://
orhttps://
?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.
Maybe it works if prefix is only added if arguments start with
/
. I'll experiment a bit with this. Otherwise option would be to parse URL or add another parameter.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.
Yep, I think pivoting on whether the first character is
/
would be fine here!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 implemented this with slash checking. Wrong adding of prefix of external prefix was shown in test cases. Hope it is correct now.