-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.go
73 lines (66 loc) · 1.63 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tinyrouter_test
import (
"fmt"
"log"
"net/http"
tiny "go101.org/tinyrouter"
)
func Example() {
routes := []tiny.Route{
{
Method: "GET",
Pattern: "/a/b/:c",
HandleFunc: func(w http.ResponseWriter, req *http.Request) {
params := tiny.PathParams(req)
fmt.Fprintln(w, "/a/b/:c", "c =", params.Value("c"))
},
},
{
Method: "GET",
Pattern: "/a/:b/c",
HandleFunc: func(w http.ResponseWriter, req *http.Request) {
params := tiny.PathParams(req)
fmt.Fprintln(w, "/a/:b/c", "b =", params.Value("b"))
},
},
{
Method: "GET",
Pattern: "/a/:b/:c",
HandleFunc: func(w http.ResponseWriter, req *http.Request) {
params := tiny.PathParams(req)
fmt.Fprintln(w, "/a/:b/:c", "b =", params.Value("b"), "c =", params.Value("c"))
},
},
{
Method: "GET",
Pattern: "/:a/b/c",
HandleFunc: func(w http.ResponseWriter, req *http.Request) {
params := tiny.PathParams(req)
fmt.Fprintln(w, "/:a/b/c", "a =", params.Value("a"))
},
},
{
Method: "GET",
Pattern: "/:a/:b/:c",
HandleFunc: func(w http.ResponseWriter, req *http.Request) {
params := tiny.PathParams(req)
fmt.Fprintln(w, "/:a/:b/:c", "a =", params.Value("a"), "b =", params.Value("b"), "c =", params.Value("c"))
},
},
}
router := tiny.New(tiny.Config{Routes: routes})
log.Println("Starting service ...")
log.Fatal(http.ListenAndServe(":8080", router))
/*
$ curl localhost:8080/a/b/c
/a/b/:c c = c
$ curl localhost:8080/a/x/c
/a/:b/c b = x
$ curl localhost:8080/a/x/y
/a/:b/:c b = x c = y
$ curl localhost:8080/x/b/c
/:a/b/c a = x
$ curl localhost:8080/x/y/z
/:a/:b/:c a = x b = y c = z
*/
}