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

Proxy middleware: add ModifyRequest function to modify http.Request before proxy #2043

Closed
wants to merge 2 commits into from

Conversation

binhnguyenduc
Copy link

The proxy middleware currently does not allow any modification to the request, only ModifyResponse. This PR adds that option so caller can pass a custom function to be applied to requests before it hits reverse proxy.

I have also added passing test.

Please consider merging this as it would allow for more flexibility when proxy with Echo.

Thanks!

@@ -1,4 +1,4 @@
module github.com/labstack/echo/v4
module github.com/binhnguyenduc/echo/v4
Copy link
Contributor

Choose a reason for hiding this comment

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

this has to be removed

@@ -54,6 +54,9 @@ type (
// Examples: If custom TLS certificates are required.
Transport http.RoundTripper

// ModifyRequest defines function to modify request before passing to ReverseProxy
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Transport field provides similar functionality as you can create your own http.RoundTripper which modifies request:

type MyRoundTripper struct {
	accessToken string
	r           http.RoundTripper
}

func (mrt MyRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
	r.Header.Add("Authorization", "Bearer: "+mrt.accessToken)
	return mrt.r.RoundTrip(r)
}

func main() {
	e := echo.New()

	e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
		Transport: MyRoundTripper{},
	}))

	log.Fatal(e.Start(":8080"))
}

https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go

@asad-awadia
Copy link

@aldas is it possible to modify the proxy target urls at runtime?

@aldas
Copy link
Contributor

aldas commented Feb 14, 2022

I think you are looking for:

ProxyBalancer interface {
and look how commonBalancer works

@asad-awadia
Copy link

@aldas oh so I can use the returned object from NewRoundRobinBalancer to change the proxy targets by calling the appropriate methods?

The interface is already implemented internally? I just have to call addTarget with the right parameter ?

@aldas aldas closed this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants