Skip to content

Instantly share code, notes, and snippets.

@SethBuilder
Created December 27, 2019 13:54
Show Gist options
  • Save SethBuilder/c993b03c5d5b1a8502f0e62c3cf78df6 to your computer and use it in GitHub Desktop.
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.
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