mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-20 04:13:39 +00:00
feat(api): add method whitelist
This commit is contained in:
parent
682b265d3e
commit
d11da6fa54
|
@ -23,6 +23,7 @@ func NewRouter(conf *config.Config) *http.ServeMux {
|
||||||
var handler http.Handler
|
var handler http.Handler
|
||||||
|
|
||||||
handler = route.HandlerFunc(conf)
|
handler = route.HandlerFunc(conf)
|
||||||
|
handler = middleware.Method(handler, route.Methods)
|
||||||
handler = middleware.Cors(handler)
|
handler = middleware.Cors(handler)
|
||||||
handler = middleware.Logger(handler)
|
handler = middleware.Logger(handler)
|
||||||
handler = middleware.Tracing(nextRequestID)(handler)
|
handler = middleware.Tracing(nextRequestID)(handler)
|
||||||
|
|
|
@ -11,7 +11,7 @@ type HandlerFunc func(conf *config.Config) http.Handler
|
||||||
|
|
||||||
// Route is the structure of an HTTP route definition
|
// Route is the structure of an HTTP route definition
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Method string
|
Methods []string
|
||||||
Path string
|
Path string
|
||||||
HandlerFunc HandlerFunc
|
HandlerFunc HandlerFunc
|
||||||
}
|
}
|
||||||
|
@ -21,17 +21,17 @@ type Routes []Route
|
||||||
|
|
||||||
var routes = Routes{
|
var routes = Routes{
|
||||||
Route{
|
Route{
|
||||||
"GET",
|
[]string{"GET", "POST"},
|
||||||
"/",
|
"/",
|
||||||
index,
|
index,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
"GET",
|
[]string{"GET"},
|
||||||
"/healtz",
|
"/healtz",
|
||||||
healthz,
|
healthz,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
"GET",
|
[]string{"GET"},
|
||||||
"/varz",
|
"/varz",
|
||||||
varz,
|
varz,
|
||||||
},
|
},
|
||||||
|
|
23
pkg/middleware/method.go
Normal file
23
pkg/middleware/method.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Method is a middleware to check that the request use the correct HTTP method
|
||||||
|
func Method(inner http.Handler, methods []string) 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 {
|
||||||
|
inner.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(405)
|
||||||
|
w.Write([]byte("405 Method Not Allowed\n"))
|
||||||
|
return
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user