-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_mount_test.go
58 lines (44 loc) · 1.84 KB
/
example_mount_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
48
49
50
51
52
53
54
55
56
57
58
package gorest_test
import (
"context"
"fmt"
"net/http"
"github.com/adamluzsi/gorest"
)
func ExampleMount() {
subResourceHandler := gorest.NewHandler(SubResourceController{})
resourceHandler := gorest.NewHandler(ResourceController{})
mux := http.NewServeMux()
gorest.Mount(resourceHandler, `/sub-resources`, subResourceHandler)
gorest.Mount(mux, `/resources`, resourceHandler)
// this will cause http.ServeMux to have handlers by the controller structures in hierarchy:
// GET /resources/{resourceID}/sub-resources/{sub-resourceID}
}
type ResourceController struct{}
type ContextKeyResource struct{}
func (ctrl ResourceController) ContextWithResource(ctx context.Context, resourceID string) (newContext context.Context, found bool, err error) {
// lookup Resource by id
// err out if lookup failed
// return false if not found
return context.WithValue(ctx, ContextKeyResource{}, Resource{ID: resourceID}), true, nil
}
type SubResourceController struct{}
type ContextKeySubResource struct{}
func (ctrl SubResourceController) ContextWithResource(ctx context.Context, subResourceID string) (context.Context, bool, error) {
// lookup Resource by id
// err out if lookup failed
// return false if not found
return context.WithValue(ctx, ContextKeySubResource{}, SubResource{ID: subResourceID}), true, nil
}
func (ctrl SubResourceController) Show(w http.ResponseWriter, r *http.Request) {
// have access to the top resource because the top resource handler set it for us
res := r.Context().Value(ContextKeyResource{}).(Resource)
// have access to the sub resource because the sub resource handler set it for us
subres := r.Context().Value(ContextKeySubResource{}).(SubResource)
// print it, because why not
_, _ = fmt.Fprintf(w, `resource: %v | subresource: %v`, res, subres)
}
type (
Resource struct{ ID string }
SubResource struct{ ID string }
)