Files
servicedemon/cmd/hub/middlewares.go

47 lines
873 B
Go

package main
import (
"fmt"
"net/http"
)
func postjson(f http.HandlerFunc) http.HandlerFunc {
return contentJSON(methodPost(f))
}
func methodPost(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, `{"errors": ["method not allowed"]}`)
return
}
f(w, req)
}
}
func contentJSON(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
headers := w.Header()
headers.Set("content-type", "application/json")
f(w, req)
}
}
type noopHandler struct {
http.HandlerFunc
}
func wrapHandleFunc(h http.HandlerFunc) noopHandler {
return noopHandler{
HandlerFunc: h,
}
}
func (h noopHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.HandlerFunc(w, req)
}