Add some conifguration and logging for the hub
This commit is contained in:
46
cmd/hub/middlewares.go
Normal file
46
cmd/hub/middlewares.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user