somehttphandlers/methodsallowed/methodsallowed.go

20 lines
410 B
Go
Raw Normal View History

2022-01-27 14:23:09 +01:00
package methodsallowed
import "net/http"
func Allow(methods ...string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, m := range methods {
if r.Method == m {
next.ServeHTTP(w, r)
return
}
}
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
}
}