March 2019
Beginner to intermediate
324 pages
7h 17m
English
As we mentioned earlier, Gin allows you to author your own middleware so that you can embed some functionality in your web app. Writing custom middleware in Gin is relatively simple, as we can see in the following steps.
The first step is to write the actual code for the middleware.
As we mentioned earlier, a web API middleware is simply an HTTP handler function that encapsulates other HTTP handler functions. Here is what the code would look like for this:
func MyCustomMiddleware() gin.HandlerFunc { return func(c *gin.Context) { //The code that is to be executed before our request gets handled, starts here // Set example variable c.Set("v", "123") //We can then retrieve the variable later via c.Get("v") // Now we our request ...Read now
Unlock full access