Skip to content

Commit a929da8

Browse files
committed
Extend routes
1 parent b743050 commit a929da8

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ fmt.Println("Starting server.")
5959
http.ListenAndServe(":8080", api)
6060
```
6161

62+
63+
## Extend routes
64+
```
65+
var user = rest.Extend("/user", api)
66+
67+
user.Use(func(ctx *rest.Context) {
68+
//User middleware will execute for /user/* routes
69+
})
70+
71+
user.Get("/:uid/profile", func(ctx *rest.Context) {
72+
ctx.JSON(`{"user": "profile"}`)
73+
})
74+
75+
user.Get("/:uid", func(ctx *rest.Context) {
76+
ctx.JSON(ctx.Params)
77+
})
78+
```
79+
80+
###Pending
81+
- Stop execution on timeout/abort
82+
6283
## Documentation
6384
https://godoc.org/github.com/go-rs/rest-api-framework
6485

examples/server.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56
"net/http"
67
"strconv"
78
"time"
@@ -43,6 +44,7 @@ func main() {
4344

4445
api.Get("/foo", func(ctx *rest.Context) {
4546
ctx.Throw("UNAUTHORIZED")
47+
// can also use ctx.ThrowWithError("SERVER_ERROR", error)
4648
})
4749

4850
// error handler
@@ -52,11 +54,15 @@ func main() {
5254

5355
fmt.Println("Starting server.")
5456

57+
tout := http.TimeoutHandler(api, 100*time.Millisecond, "timeout")
58+
5559
server := http.Server{
5660
Addr: ":8080",
57-
Handler: api,
61+
Handler: tout,
62+
}
63+
64+
if err := server.ListenAndServe(); err != nil {
65+
log.Fatalf("could not start server, %v", err)
5866
}
5967

60-
err := server.ListenAndServe()
61-
fmt.Println(err)
6268
}

examples/user/user.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66

77
func Load(api *rest.API) {
88

9-
var user rest.Namespace
10-
11-
user.Set("/user", api)
9+
var user = rest.Extend("/user", api)
1210

1311
user.Use(func(ctx *rest.Context) {
1412
println("User middleware > /user/*")

namespace.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
package rest
66

77
import (
8-
"github.com/go-rs/rest-api-framework/utils"
98
"net/http"
9+
10+
"github.com/go-rs/rest-api-framework/utils"
1011
)
1112

1213
// Namespace is used to extend routes with a specific prefix
@@ -16,10 +17,12 @@ type Namespace struct {
1617
}
1718

1819
// Set method is used to set prefix with API
19-
func (n *Namespace) Set(prefix string, api *API) {
20+
func Extend(prefix string, api *API) *Namespace {
2021
prefix = utils.TrimSuffix(prefix, "/")
21-
n.prefix = prefix
22-
n.api = api
22+
return &Namespace{
23+
prefix: prefix,
24+
api: api,
25+
}
2326
}
2427

2528
// Use method is used to set interceptors/middlewares for all requests,

namespace_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
var api API
12-
var ns Namespace
12+
var ns *Namespace
1313
var handle Handler
1414

15-
func TestNamespace_Set(t *testing.T) {
16-
ns.Set("/test", &api)
15+
func TestExtend(t *testing.T) {
16+
ns = Extend("/test", &api)
1717

1818
if ns.prefix != "/test" {
1919
t.Error("Prefix is not set.")

0 commit comments

Comments
 (0)