-
Notifications
You must be signed in to change notification settings - Fork 1
/
gorilla.go
58 lines (53 loc) · 1.88 KB
/
gorilla.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gosimplerest
import (
"database/sql"
"net/http"
"strings"
"github.com/franciscoescher/gosimplerest/handlers"
"github.com/franciscoescher/gosimplerest/resource"
"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
func AddGorillaMuxHandlers(r *mux.Router, d *sql.DB, l *logrus.Logger, v *validator.Validate,
resources []resource.Resource, mid func(h http.Handler) http.HandlerFunc) *mux.Router {
h := AddRouteFunctions{
Post: GorillaAddRouteFunc(r, mid, http.MethodPost),
Get: GorillaAddRouteFunc(r, mid, http.MethodGet),
Put: GorillaAddRouteFunc(r, mid, http.MethodPut),
Patch: GorillaAddRouteFunc(r, mid, http.MethodPatch),
Delete: GorillaAddRouteFunc(r, mid, http.MethodDelete),
Head: GorillaAddRouteFunc(r, mid, http.MethodHead),
}
apf := func(name string, param string) string {
var sb strings.Builder
sb.WriteString(name)
sb.WriteString("/{")
sb.WriteString(param)
sb.WriteString("}")
return sb.String()
}
AddHandlers(d, l, v, h, apf, resources)
return r
}
// GorillaAddRouteFunc is used to add a route to the router, using the given method.
// It adds the middleware function to the handler.
// It adds params to request context.
func GorillaAddRouteFunc(r *mux.Router, mid func(h http.Handler) http.HandlerFunc, method string) AddRouteFunc {
return func(name string, h http.HandlerFunc) {
r.HandleFunc(name, GorillaHandlerWrapper(mid, h)).Methods(method)
}
}
// GorillaHandlerWrapper wraps the handler function.
// It adds params to request context.
// If the middelware function is nil, it returns the handler
func GorillaHandlerWrapper(mid func(h http.Handler) http.HandlerFunc, h http.HandlerFunc) http.HandlerFunc {
if mid != nil {
h = mid(h)
}
return func(rw http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
r = handlers.GetRequestWithParams(r, params)
h.ServeHTTP(rw, r)
}
}