package
0.0.0-20241023215409-3963077d6d8b
Repository: https://github.com/yardbirdsax/go-all-in.git
Documentation: pkg.go.dev
# README
Middleware in Golang
This folder contains my notes and examples of how to write HTTP middleware in Go.
It uses the the standard net/http
package, zap
for logging, and Alice
for easier chaining of middleware.
References:
- https://medium.com/@matryer/the-http-handler-wrapper-technique-in-golang-updated-bc7fbcffa702#.e4k81jxd3
- https://www.nicolasmerouze.com/middlewares-golang-best-practices-examples
Middleware
Currently there are three middleware methods:
LogMiddleware
- logs a start request and end request message.ResponseMesserMiddleware
- prepends a naughty note to the body of the response by intercepting theResponseWriter
object and redirecting its writes.AddHeaderMiddleware
- adds an arbitrary header value to the response. This was interesting because I wanted to make the method generic while still making it compatible with Alice, so I actually had to write a method that returned the method expected by Alice'sNew
method.
Testing
All three middlewares have unit tests, which are written in the style of "black-box" tests (i.e. they are written using only the public interfaces of the package). See this GitHub issue and the linked StackOverflow answer for more details on this pattern.
Additionally, you can see some integration testing of the implementations of the middleware in the API in the api/integration folder.
# Functions
This lets us add an arbitrary header key and value.
Log some stuff.
No description provided by the author
Do some things to the response.
# Structs
No description provided by the author