File tree Expand file tree Collapse file tree 5 files changed +41
-13
lines changed Expand file tree Collapse file tree 5 files changed +41
-13
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,27 @@ fmt.Println("Starting server.")
5959http.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
6384https://godoc.org/github.com/go-rs/rest-api-framework
6485
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package main
22
33import (
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}
Original file line number Diff line number Diff line change 66
77func 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/*" )
Original file line number Diff line number Diff line change 55package rest
66
77import (
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,
Original file line number Diff line number Diff line change @@ -9,11 +9,11 @@ import (
99)
1010
1111var api API
12- var ns Namespace
12+ var ns * Namespace
1313var 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." )
You can’t perform that action at this time.
0 commit comments