Skip to content

Commit b68cad2

Browse files
committed
Introduce shortcuts to create Routes
The four new methods Get, Post, Put and Delete are simple constructors for the Route objects. Intantiating the Route object directly is still supported, and allows the framework to handle any HTTP method. These shortcuts are there to save keystrokes, improve readability and to play nicely with golint. (which likes to have key typed in the literals)
1 parent fc0d6ba commit b68cad2

File tree

7 files changed

+101
-64
lines changed

7 files changed

+101
-64
lines changed

rest/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// api := rest.NewApi()
3535
// api.Use(rest.DefaultDevStack...)
3636
// router, err := rest.MakeRouter(
37-
// rest.Route{"GET", "/users/:id", GetUser},
37+
// rest.Get("/users/:id", GetUser),
3838
// )
3939
// if err != nil {
4040
// log.Fatal(err)

rest/gzip_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ func TestGzipEnabled(t *testing.T) {
1414

1515
// router app with success and error paths
1616
router, err := MakeRouter(
17-
&Route{"GET", "/ok",
18-
func(w ResponseWriter, r *Request) {
19-
w.WriteJson(map[string]string{"Id": "123"})
20-
},
21-
},
22-
&Route{"GET", "/error",
23-
func(w ResponseWriter, r *Request) {
24-
Error(w, "gzipped error", 500)
25-
},
26-
},
17+
Get("/ok", func(w ResponseWriter, r *Request) {
18+
w.WriteJson(map[string]string{"Id": "123"})
19+
}),
20+
Get("/error", func(w ResponseWriter, r *Request) {
21+
Error(w, "gzipped error", 500)
22+
}),
2723
)
2824
if err != nil {
2925
t.Fatal(err)
@@ -53,11 +49,9 @@ func TestGzipDisabled(t *testing.T) {
5349

5450
// router app with success and error paths
5551
router, err := MakeRouter(
56-
&Route{"GET", "/ok",
57-
func(w ResponseWriter, r *Request) {
58-
w.WriteJson(map[string]string{"Id": "123"})
59-
},
60-
},
52+
Get("/ok", func(w ResponseWriter, r *Request) {
53+
w.WriteJson(map[string]string{"Id": "123"})
54+
}),
6155
)
6256
if err != nil {
6357
t.Fatal(err)

rest/handler_test.go

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,29 @@ func TestHandler(t *testing.T) {
1515
ErrorLogger: log.New(ioutil.Discard, "", 0),
1616
}
1717
handler.SetRoutes(
18-
&Route{"GET", "/r/:id",
19-
func(w ResponseWriter, r *Request) {
20-
id := r.PathParam("id")
21-
w.WriteJson(map[string]string{"Id": id})
22-
},
23-
},
24-
&Route{"POST", "/r/:id",
25-
func(w ResponseWriter, r *Request) {
26-
// JSON echo
27-
data := map[string]string{}
28-
err := r.DecodeJsonPayload(&data)
29-
if err != nil {
30-
t.Fatal(err)
31-
}
32-
w.WriteJson(data)
33-
},
34-
},
35-
&Route{"GET", "/auto-fails",
36-
func(w ResponseWriter, r *Request) {
37-
a := []int{}
38-
_ = a[0]
39-
},
40-
},
41-
&Route{"GET", "/user-error",
42-
func(w ResponseWriter, r *Request) {
43-
Error(w, "My error", 500)
44-
},
45-
},
46-
&Route{"GET", "/user-notfound",
47-
func(w ResponseWriter, r *Request) {
48-
NotFound(w, r)
49-
},
50-
},
18+
Get("/r/:id", func(w ResponseWriter, r *Request) {
19+
id := r.PathParam("id")
20+
w.WriteJson(map[string]string{"Id": id})
21+
}),
22+
Post("/r/:id", func(w ResponseWriter, r *Request) {
23+
// JSON echo
24+
data := map[string]string{}
25+
err := r.DecodeJsonPayload(&data)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
w.WriteJson(data)
30+
}),
31+
Get("/auto-fails", func(w ResponseWriter, r *Request) {
32+
a := []int{}
33+
_ = a[0]
34+
}),
35+
Get("/user-error", func(w ResponseWriter, r *Request) {
36+
Error(w, "My error", 500)
37+
}),
38+
Get("/user-notfound", func(w ResponseWriter, r *Request) {
39+
NotFound(w, r)
40+
}),
5141
)
5242

5343
// valid get resource

rest/jsonp_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ func TestJsonpMiddleware(t *testing.T) {
1414

1515
// router app with success and error paths
1616
router, err := MakeRouter(
17-
&Route{"GET", "/ok",
18-
func(w ResponseWriter, r *Request) {
19-
w.WriteJson(map[string]string{"Id": "123"})
20-
},
21-
},
22-
&Route{"GET", "/error",
23-
func(w ResponseWriter, r *Request) {
24-
Error(w, "jsonp error", 500)
25-
},
26-
},
17+
Get("/ok", func(w ResponseWriter, r *Request) {
18+
w.WriteJson(map[string]string{"Id": "123"})
19+
}),
20+
Get("/error", func(w ResponseWriter, r *Request) {
21+
Error(w, "jsonp error", 500)
22+
}),
2723
)
2824
if err != nil {
2925
t.Fatal(err)

rest/route.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,39 @@ func (route *Route) MakePath(pathParams map[string]string) string {
3535
}
3636
return path
3737
}
38+
39+
// Get is a shortcut method that instantiates a GET route. Equivalent to &Route{"GET", pathExp, handlerFunc}
40+
func Get(pathExp string, handlerFunc HandlerFunc) *Route {
41+
return &Route{
42+
HttpMethod: "GET",
43+
PathExp: pathExp,
44+
Func: handlerFunc,
45+
}
46+
}
47+
48+
// Post is a shortcut method that instantiates a POST route. Equivalent to &Route{"POST", pathExp, handlerFunc}
49+
func Post(pathExp string, handlerFunc HandlerFunc) *Route {
50+
return &Route{
51+
HttpMethod: "POST",
52+
PathExp: pathExp,
53+
Func: handlerFunc,
54+
}
55+
}
56+
57+
// Put is a shortcut method that instantiates a PUT route. Equivalent to &Route{"PUT", pathExp, handlerFunc}
58+
func Put(pathExp string, handlerFunc HandlerFunc) *Route {
59+
return &Route{
60+
HttpMethod: "PUT",
61+
PathExp: pathExp,
62+
Func: handlerFunc,
63+
}
64+
}
65+
66+
// Delete is a shortcut method that instantiates a DELETE route. Equivalent to &Route{"DELETE", pathExp, handlerFunc}
67+
func Delete(pathExp string, handlerFunc HandlerFunc) *Route {
68+
return &Route{
69+
HttpMethod: "DELETE",
70+
PathExp: pathExp,
71+
Func: handlerFunc,
72+
}
73+
}

rest/route_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,26 @@ func TestReverseRouteResolution(t *testing.T) {
4848
t.Errorf("expected %s, got %s", expected, got)
4949
}
5050
}
51+
52+
func TestShortcutMethods(t *testing.T) {
53+
54+
r := Get("/", nil)
55+
if r.HttpMethod != "GET" {
56+
t.Errorf("expected GET, got %s", r.HttpMethod)
57+
}
58+
59+
r = Post("/", nil)
60+
if r.HttpMethod != "POST" {
61+
t.Errorf("expected POST, got %s", r.HttpMethod)
62+
}
63+
64+
r = Put("/", nil)
65+
if r.HttpMethod != "PUT" {
66+
t.Errorf("expected PUT, got %s", r.HttpMethod)
67+
}
68+
69+
r = Delete("/", nil)
70+
if r.HttpMethod != "DELETE" {
71+
t.Errorf("expected DELETE, got %s", r.HttpMethod)
72+
}
73+
}

rest/test/doc.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
// func TestSimpleRequest(t *testing.T) {
1616
// handler := ResourceHandler{}
1717
// handler.SetRoutes(
18-
// &Route{"GET", "/r",
19-
// func(w ResponseWriter, r *Request) {
20-
// w.WriteJson(map[string]string{"Id": "123"})
21-
// },
22-
// },
18+
// Get("/r", func(w ResponseWriter, r *Request) {
19+
// w.WriteJson(map[string]string{"Id": "123"})
20+
// }),
2321
// )
2422
// recorded := test.RunRequest(t, &handler,
2523
// test.MakeSimpleRequest("GET", "http://1.2.3.4/r", nil))

0 commit comments

Comments
 (0)