Closed arpitjindal97 closed 2 years ago
Yeh, you maybe use the http middleware and add it into the http router, for example,
func CORS() middleware.Middleware {
return middleware.NewMiddleware("CORS", 123, func(h interface{}) interface {
return http.HandlerFunc(func(w ResponseWriter, r *Request) {
// TODO: Add some headers for the respsone
w. Header().Set("Access-Control-Allow-Origin", "*")
h.(http.Handler).ServeHTTP(w, r)
})
}
}
router.DefaultRouter.User(CORS())
router.DefaultRouter. RouteManager = ruler.DefaultRouter
ruler.DefaultRouter.Path("/path/to/1").GET(GetHandler)
ruler.DefaultRouter.Path("/path/to/2").POST(PostHandler)
Later, I will port the CORS
and Gzip
middlewares from ship
.
I have added the http middlewares CORS
and GZip
. You can try v0.0.0-20220822143418-03eab6495b5c
and I will tag it with v0.21.0
later. For example
// Use the middleware CORS and GZip.
router.DefaultRouter.Middlewares.Use(middlewares.CORS(123, nil), middlewares.GZip(456, nil))
// Use the rule router to manage the routes, which has been done by default.
// Here is only an example.
router.DefaultRouter.RouteManager = ruler.DefaultRouter
// Add the routes
ruler.DefaultRouter.Path("/path/to/1").GET(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO:
}))
ruler.DefaultRouter.Path("/path/to/2").GETFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO:
})
ruler.DefaultRouter.Path("/path/to/3").GETContext(func(ctx *reqresp.Context) {
// TODO:
})
ruler.DefaultRouter.Path("/path/to/4").GETContextWithError(func(ctx *reqresp.Context) error {
// TODO:
return nil
})
There are few headers which I want to set on all of my endpoints.
ex: Access-Control-Allow-Origin
I know it can be set individually in the
HandlerFunc
but is there a way to set them globally ?