feat: add rest.WithCustomCors to let caller customize the response (#1274)

This commit is contained in:
Kevin Wan
2021-11-25 23:03:37 +08:00
committed by GitHub
parent 86f9f63b46
commit 0395ba1816
4 changed files with 72 additions and 6 deletions

View File

@@ -99,8 +99,17 @@ func ToMiddleware(handler func(next http.Handler) http.Handler) Middleware {
// WithCors returns a func to enable CORS for given origin, or default to all origins (*).
func WithCors(origin ...string) RunOption {
return func(server *Server) {
server.router.SetNotAllowedHandler(cors.NotAllowedHandler(origin...))
server.Use(cors.Middleware(origin...))
server.router.SetNotAllowedHandler(cors.NotAllowedHandler(nil, origin...))
server.Use(cors.Middleware(nil, origin...))
}
}
// WithCustomCors returns a func to enable CORS for given origin, or default to all origins (*),
// fn lets caller customizing the response.
func WithCustomCors(fn func(http.ResponseWriter), origin ...string) RunOption {
return func(server *Server) {
server.router.SetNotAllowedHandler(cors.NotAllowedHandler(fn, origin...))
server.Use(cors.Middleware(fn, origin...))
}
}