-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_unshift_test.go
47 lines (38 loc) · 1.4 KB
/
example_unshift_test.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
package gorest_test
import (
"context"
"fmt"
"net/http"
"github.com/adamluzsi/gorest"
)
func ExampleUnshiftPathParamFromRequest() {
var withResourceID = func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r, param := gorest.UnshiftPathParamFromRequest(r)
// verify Resource id is valid and present
// verify authority of the requester to this resourceID
r = r.WithContext(context.WithValue(r.Context(), exampleUnshiftCtxKeyResourceID{}, param)) // add request context
next.ServeHTTP(w, r)
})
}
mux := http.NewServeMux()
mux.Handle(`/routes/`, http.StripPrefix(`/routes`, withResourceID(NewResourceHandler())))
}
type exampleUnshiftCtxKeyResourceID struct{}
type ExampleUnshiftHTTPHandler struct {
ServeMux *http.ServeMux
}
func NewResourceHandler() *ExampleUnshiftHTTPHandler {
h := &ExampleUnshiftHTTPHandler{ServeMux: http.NewServeMux()}
h.ServeMux.HandleFunc(`/show`, h.show)
return h
}
// http path for this is /routes/:resource_id/show
// but anything is fine as long the context has the resourceID
func (h *ExampleUnshiftHTTPHandler) show(w http.ResponseWriter, r *http.Request) {
resourceID := r.Context().Value(exampleUnshiftCtxKeyResourceID{}).(string)
_, _ = fmt.Fprintf(w, `path param is: %s`, resourceID)
}
func (h *ExampleUnshiftHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.ServeMux.ServeHTTP(w, r)
}