Skip to content

Commit

Permalink
Closes labstack#104
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Nov 22, 2015
1 parent 2ad06ce commit 9d11990
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (

"bytes"

"golang.org/x/net/context"
"golang.org/x/net/websocket"
)

type (
// Context represents context for the current request. It holds request and
// response objects, path parameters, data and registered handler.
Context struct {
context.Context
request *http.Request
response *Response
socket *websocket.Conn
Expand Down
8 changes: 8 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"strings"

"golang.org/x/net/context"

"net/url"

"encoding/xml"
Expand Down Expand Up @@ -271,6 +273,12 @@ func TestContextForm(t *testing.T) {
assert.Equal(t, "[email protected]", c.Form("email"))
}

func TestContextNetContext(t *testing.T) {
c := new(Context)
c.Context = context.WithValue(nil, "key", "val")
assert.Equal(t, "val", c.Value("key"))
}

func testBind(t *testing.T, c *Context, ct string) {
c.request.Header.Set(ContentType, ct)
u := new(user)
Expand Down
28 changes: 23 additions & 5 deletions website/content/guide/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ menu:

### Handler path

`Context.Path()` returns the registered path for a handler, it can be used in the middleware for logging purpose.
`Context#Path()` returns the registered path for a handler, it can be used in the
middleware for logging purpose.

*Example*

Expand All @@ -22,10 +23,27 @@ e.Get("/users/:name", func(c *echo.Context) error) {
})
```

### golang.org/x/net/context

`echo.Context` embeds `context.Context` interface, so all it's properties
are available right from `echo.Context`.

*Example*

```go
e.Get("/users/:name", func(c *echo.Context) error) {
c.Context = context.WithValue(nil, "key", "val")
// Pass it down...
// Use it...
println(c.Value("key"))
return c.String(http.StatusOK, name)
})
```

### Path parameter

Path parameter can be retrieved either by name `Context.Param(name string) string`
or by index `Context.P(i int) string`. Getting parameter by index gives a slightly
Path parameter can be retrieved either by name `Context#Param(name string) string`
or by index `Context#P(i int) string`. Getting parameter by index gives a slightly
better performance.

*Example*
Expand All @@ -48,7 +66,7 @@ $ curl http://localhost:1323/users/joe

### Query parameter

Query parameter can be retrieved by name using `Context.Query(name string)`.
Query parameter can be retrieved by name using `Context#Query(name string)`.

*Example*

Expand All @@ -65,7 +83,7 @@ $ curl -G -d "name=joe" http://localhost:1323/users

### Form parameter

Form parameter can be retrieved by name using `Context.Form(name string)`.
Form parameter can be retrieved by name using `Context#Form(name string)`.

*Example*

Expand Down

0 comments on commit 9d11990

Please sign in to comment.