-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
51 lines (41 loc) · 1.31 KB
/
example_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
package rahjoo_test
import (
"log"
"net/http"
"github.com/amirzayi/rahjoo"
"github.com/amirzayi/rahjoo/middleware"
)
func ExampleNewGroupRoute() {
listUsers := func(http.ResponseWriter, *http.Request) {}
_ = rahjoo.NewGroupRoute("/api/v1", rahjoo.Route{
"/users": {
http.MethodGet: rahjoo.NewHandler(listUsers),
},
"/post/{id}/users": {
http.MethodGet: rahjoo.NewHandler(listUsers, middleware.Recovery(log.Default())),
},
}).SetMiddleware(middleware.EnforceJSON)
}
func ExampleNewHandler() {
h := func(w http.ResponseWriter, r *http.Request) {}
rahjoo.NewHandler(h, middleware.EnforceJSON, middleware.Recovery(log.Default()))
}
func ExampleBindRoutesToMux() {
listUsers := func(http.ResponseWriter, *http.Request) {}
userV1Gp := rahjoo.NewGroupRoute("/api/v1/users", rahjoo.Route{
"/list": {
http.MethodGet: rahjoo.NewHandler(listUsers, middleware.EnforceJSON),
},
"/{id}": {
http.MethodGet: rahjoo.NewHandler(http.NotFound),
},
})
userV2Gp := rahjoo.NewGroupRoute("/api/v2", rahjoo.Route{
"/users": {
"": rahjoo.NewHandler(listUsers),
},
}).SetMiddleware(middleware.EnforceJSON, middleware.Recovery(log.Default()))
mux := http.NewServeMux()
mux.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("./images"))))
rahjoo.BindRoutesToMux(mux, userV1Gp, userV2Gp)
}