support cors in rest server
This commit is contained in:
29
rest/handlers.go
Normal file
29
rest/handlers.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
allowOrigin = "Access-Control-Allow-Origin"
|
||||
allOrigin = "*"
|
||||
allowMethods = "Access-Control-Allow-Methods"
|
||||
allowHeaders = "Access-Control-Allow-Headers"
|
||||
headers = "Content-Type, Content-Length, Origin"
|
||||
methods = "GET, HEAD, POST, PATCH, PUT, DELETE"
|
||||
separator = ", "
|
||||
)
|
||||
|
||||
func CorsHandler(origins ...string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if len(origins) > 0 {
|
||||
w.Header().Set(allowOrigin, strings.Join(origins, separator))
|
||||
} else {
|
||||
w.Header().Set(allowOrigin, allOrigin)
|
||||
}
|
||||
w.Header().Set(allowMethods, methods)
|
||||
w.Header().Set(allowHeaders, headers)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user