Data server decorators implementation.

Co-authored-by: Hector Hurtado <hector.hurtado@bbva.com>
This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-10-21 17:54:47 +02:00
parent 63cd125327
commit 5eae018ee6
3 changed files with 178 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
package data
import (
"net/http"
"github.com/BBVA/kapow/internal/server/model"
"github.com/gorilla/mux"
)
type resourceHandler func(http.ResponseWriter, *http.Request, *model.Handler)
func lockResponseWriter(fn resourceHandler) resourceHandler {
return func(w http.ResponseWriter, r *http.Request, h *model.Handler) {
h.Writing.Lock()
defer h.Writing.Unlock()
fn(w, r, h)
}
}
func checkHandler(fn resourceHandler) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
handlerID := mux.Vars(r)["handlerID"]
if h, ok := Handlers.Get(handlerID); ok {
fn(w, r, h)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}