gin icon indicating copy to clipboard operation
gin copied to clipboard

feat(backwards compatible): support for onredirect middleware

Open yashvardhan-kukreja opened this issue 11 months ago • 0 comments

Resolves #3857

Example:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-contrib/cors"
)

func main() {
	g := gin.Default()
        g.OnRedirect(func(c *gin.Context) {
                     c.Writer.Header.Set("X-Foo", "Bar")
        })
	g.GET("/foo", func(c *gin.Context) {
		c.String(200, "Hello, World!"))
	})
	g.Run(":9000")
}

Calling this server at GET /foo/ responds with a 307 Response with Location: /foo and Response Header as X-Foo: Bar;

Just like this, even CORS-mitigation headers can be applied as well

A note on backwards compartibility

This PR complies with backwards compatibility by ensuring that the behaviour of a Gin with no explicitly attached redirectMethod-HandlerChain to the engine identically works like it worked in the past.

Only when a developer explicitly uses the .OnRedirect method on their GIn engine makes them leverage the functionalities offered by this PR

yashvardhan-kukreja avatar Feb 29 '24 00:02 yashvardhan-kukreja