Gorouter is an Onion
What
In the last story you learned that there are handlers that act as middleware in gorouter. But how do these handlers work?
Gorouter is like an onion This middleware is organized like an onion. A request goes through all of the middleware layers to get to the app (the center of the onion). Then the app sends a response, which goes through all the layers again in reverse order. This means that the handlers have two chances to do things during each request/response: once when the request is on the way to the app and once when the response in on the way back to the client.
But what does this look like in the code?
Each handler has a function with the signature: ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
. Inside the function each handler calls next(rw, r)
. The stuff inside the function before the next(rw, r)
is done when request is at this handler on its way to the app. The stuff inside the function after the next(rw, r)
is done once the response has made it back to this handler on its way back to the client. Some handlers only do things when the request is on its way to the app. Some handlers only do things when the response is returning to the client. Some handlers do stuff in both cases.
Handlers need to share information In order for handlers to pass information between each other there is a Request Info struct. There is one Request Info struct per request. The handlers can look up this struct using the request. See here for an example in the lookup handler.
How
🤔 Examine the lookup handler
- Look at the lookup handler. ❓ What does this handler do? ❓ What does this handler set on the Request Info struct? ❓ When does this handler do its logic? When the request is on its way to the app or when the response is on its way to the client? Or both? How do you know?
Extra Credit
🤔 Find another handler that only contains logic that occurs when the request is on its way to the app. 🤔 Find a handler that only contains logic that occurs when the response is on its way to the client.