webhookd/pkg/middleware/methods.go
Nicolas Carlier 6b3623f67a feat(api): refactore router
- Simplify router
- Single routes onfiguration file
- Improve HTTP logger
2021-07-11 13:09:44 +02:00

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"))
})
}
}