-
Notifications
You must be signed in to change notification settings - Fork 1
/
chi.go
63 lines (57 loc) · 1.7 KB
/
chi.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
59
60
61
62
63
package gosimplerest
import (
"database/sql"
"net/http"
"strings"
"github.com/franciscoescher/gosimplerest/handlers"
"github.com/franciscoescher/gosimplerest/resource"
"github.com/go-chi/chi"
"github.com/go-playground/validator/v10"
"github.com/sirupsen/logrus"
)
func AddChiHandlers(r *chi.Mux, d *sql.DB, l *logrus.Logger, v *validator.Validate, resources []resource.Resource) *chi.Mux {
h := AddRouteFunctions{
Post: ChiAddRouteFunc(r.Post),
Get: ChiAddRouteFunc(r.Get),
Put: ChiAddRouteFunc(r.Put),
Patch: ChiAddRouteFunc(r.Patch),
Delete: ChiAddRouteFunc(r.Delete),
Head: ChiAddRouteFunc(r.Head),
}
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
}
// ChiAddRouteFunc uses the f function to add a route to the router,
// wrapping the handler to add params to request context.
func ChiAddRouteFunc(f AddRouteFunc) AddRouteFunc {
return func(name string, h http.HandlerFunc) {
f(name, ChiHandlerWrapper(h))
}
}
// ChiHandlerWrapper wraps the handler function.
// It adds params to request context.
func ChiHandlerWrapper(h http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
// get params from chi context
keys := make([]string, 0)
values := make([]string, 0)
rctx := chi.RouteContext(r.Context())
if rctx != nil {
keys = rctx.URLParams.Keys
values = rctx.URLParams.Values
}
params := make(map[string]string, len(keys))
for i := range keys {
params[keys[i]] = values[i]
}
handlers.AddParamsToHandlerFunc(h, params)(rw, r)
}
}