Middleware is an important and fun topic in the world of modern web applications. The word middleware can mean many things in the software development industry. However, for the purpose of this book, we only care about one definition for it. Middleware can simply be defined as code that can run between the time you receive an HTTP request and the time your handler code gets executed on that request. This is best explained through an example.
In the RESTful API that we built for our GoMusic application, let's pick on one of our API endpoints—the /products relative URL. Here was the code that was used to assign this relative URL to an action or a function handler:
r.GET("/products", h.GetProducts)
Here was the code of the GetProducts handler:
func (h *Handler) GetProducts(c *gin.Context) {
if h.db == nil {
return
}
products, err := h.db.GetAllProducts...