mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
feat: support rest.WithCorsHeaders to customize cors headers (#4284)
This commit is contained in:
@@ -161,6 +161,18 @@ func WithCors(origin ...string) RunOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithCorsHeaders returns a RunOption to enable CORS with given headers.
|
||||
func WithCorsHeaders(headers ...string) RunOption {
|
||||
const allDomains = "*"
|
||||
|
||||
return func(server *Server) {
|
||||
server.router.SetNotAllowedHandler(cors.NotAllowedHandler(nil, allDomains))
|
||||
server.router = newCorsRouter(server.router, func(header http.Header) {
|
||||
cors.AddAllowHeaders(header, headers...)
|
||||
}, allDomains)
|
||||
}
|
||||
}
|
||||
|
||||
// WithCustomCors returns a func to enable CORS for given origin, or default to all origins (*),
|
||||
// fn lets caller customizing the response.
|
||||
func WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(http.ResponseWriter),
|
||||
|
||||
Reference in New Issue
Block a user