mirror of
https://github.com/ncarlier/webhookd.git
synced 2024-09-20 17:05:27 +00:00
25 lines
585 B
Go
25 lines
585 B
Go
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// Methods is a middleware to check that the request use the correct HTTP method
|
||
|
func Methods(methods ...string) Middleware {
|
||
|
return func(next http.Handler) http.Handler {
|
||
|
allowedMethods := make(map[string]struct{}, len(methods))
|
||
|
for _, s := range methods {
|
||
|
allowedMethods[s] = struct{}{}
|
||
|
}
|
||
|
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
if _, ok := allowedMethods[r.Method]; ok {
|
||
|
next.ServeHTTP(w, r)
|
||
|
return
|
||
|
}
|
||
|
w.WriteHeader(405)
|
||
|
w.Write([]byte("405 Method Not Allowed\n"))
|
||
|
})
|
||
|
}
|
||
|
}
|