gin gonic redirect page code example

Example: gin middleware redirect

//This is auth redirect example
func my_middleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		_, err := c.Request.Cookie("auth")
		if err == http.ErrNoCookie {
			c.Redirect(http.StatusTemporaryRedirect, "example.com/redirect")
			c.AbortWithStatus(http.StatusTemporaryRedirect)
		}
		if err != nil {
			// some other error
			http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
			c.AbortWithStatus(http.StatusInternalServerError)
		}
		c.Next()
	}
}

Tags:

Go Example