You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a recent project, I wanted to apply CORs headers on just one one path.
I couldn't achieve my objective by adding cors middleware to a gin.RouterGroup, because I had other important, cookie-reading, global middleware I wanted to add to every request, that would run before cors.
I used something like this and used it as the first middleware on the gin.Engine:
// CORS needs to be the first middleware called for endpoints that enable it. This middleware allows specifying which paths have CORs enabled. CORS headers will be set for those paths.funcCORSFor(match_paths []string) gin.HandlerFunc {
corsHandler:=cors.New(cors.Config{
AllowMethods: []string{"POST", "GET", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{"Origin", "Cookie", arg.Config.LicenseHeader()},
AllowCredentials: true,
AllowBrowserExtensions: true,
AllowOriginFunc: func(originstring) bool {
returntrue
},
MaxAge: 24*time.Hour,
})
returnfunc(c*gin.Context) {
path:=c.Request.URL.Pathfor_, pref:=rangematch_paths {
ifstrings.HasPrefix(path, pref) {
corsHandler(c)
return
}
}
c.Next()
}
}
I thought this could be a candidate for a config feature, something like Config.MatchPaths of type []string
Could I do a PR for this feature?
Thanks
The text was updated successfully, but these errors were encountered:
The problem with such a feature is that it promotes poor separation of concerns: you would then have to worry about routes both at the router level and in the configuration of your CORS middleware.
In a recent project, I wanted to apply CORs headers on just one one path.
I couldn't achieve my objective by adding cors middleware to a gin.RouterGroup, because I had other important, cookie-reading, global middleware I wanted to add to every request, that would run before cors.
I used something like this and used it as the first middleware on the gin.Engine:
I thought this could be a candidate for a config feature, something like
Config.MatchPaths
of type []stringCould I do a PR for this feature?
Thanks
The text was updated successfully, but these errors were encountered: