http.ReverseProxy in Gorouter
What
In the last story you learned what a reverse proxy is at a high level. In this story you will look at how golang implements a reverse proxy and how gorouter uses that reverse proxy struct.
How
Read some docs
Look at Gorouter code
- Gorouter’s
main.go, like manymain.gos, is where everything is set up and initialized, but not much happens. Skim through main.go. âť“ Do you see anything interesting in there? Did anything catch your eye? -
In
main.goit initializes a new proxy. Look at the code here. -
When this new proxy is created, it creates a new http.ReverseProxy. Look at the code here.
-
Golang’s http.ReverseProxy struct has 7 configurable properties. Gorouter configures 5 of them. ❓ Which http.ReverseProxy properties does gorouter configure? ❓ Which http.ReverseProxy properties does gorouter leave alone?
-
The
meatiesttofu-iest bit of configuration that gorouter does is that it assigns its own Proxy Round Tripper to the http.ReverseProxyTransport. Look at the code here. - This Proxy Round Tripper handles gorouter’s custom routing logic and is a wrapper around the default Transport roundTrip function. Take a quick look at the code for the Proxy Round Tripper. We will looker closer at this component in a later story.