-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
40 lines (37 loc) 路 1.22 KB
/
converter.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
package chi
import (
"net/http"
"github.com/go-chi/chi"
"github.com/pkg/errors"
)
// Converter returns the Middleware to convert chi' URL params into query params.
//
// router := chi.NewRouter()
// router.Route("/resources", func(router chi.Router) {
// router.With(middleware.Pagination).Get("/", resource.List)
// router.Post("/", resource.Create)
// router.Route("/{id}", func(router chi.Router) {
// router.Use(middleware.Converter("id", "resourceID"))
// router.Get("/", resource.Fetch)
// router.Put("/", resource.Update)
// router.Delete("/", resource.Delete)
// })
// })
// http.Handle("/", router)
//
func Converter(placeholders ...string) func(http.Handler) http.Handler {
if count := len(placeholders); count == 0 || count%2 != 0 {
panic(errors.Errorf("count of passed placeholders must be even: obtained %d", count))
}
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
for i, count := 0, len(placeholders); i < count; i += 2 {
from, to := placeholders[i], placeholders[i+1]
q.Set(to, chi.URLParam(req, from))
}
req.URL.RawQuery = q.Encode()
handler.ServeHTTP(rw, req)
})
}
}