Created
December 27, 2019 13:54
-
-
Save SethBuilder/c993b03c5d5b1a8502f0e62c3cf78df6 to your computer and use it in GitHub Desktop.
This is a GoLang coding exercise: Build a middleware, which checks for the http header key “test” equals value “yes” and then log “header is fine” to console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/gin-gonic/gin" | |
) | |
func middleware() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
header := c.Request.Header.Get("test") | |
if header == "yes" { | |
fmt.Println("header is fine") | |
} | |
c.Next() | |
} | |
} | |
func index(c *gin.Context) { | |
c.String(200, "hello-world") | |
} | |
func main() { | |
r := gin.Default() | |
r.Use(middleware()) | |
r.GET("/", index) | |
fmt.Println("Server starting ...") | |
r.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment