From 9d11990cbbc6e5eeed84c5d280d3a3d9ec372ad0 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 22 Nov 2015 10:38:02 -0800 Subject: [PATCH] Closes #104 Signed-off-by: Vishal Rana --- context.go | 2 ++ context_test.go | 8 ++++++++ website/content/guide/request.md | 28 +++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/context.go b/context.go index cf5797dc..73194a7b 100644 --- a/context.go +++ b/context.go @@ -12,6 +12,7 @@ import ( "bytes" + "golang.org/x/net/context" "golang.org/x/net/websocket" ) @@ -19,6 +20,7 @@ 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 diff --git a/context_test.go b/context_test.go index f44194f6..27f48989 100644 --- a/context_test.go +++ b/context_test.go @@ -10,6 +10,8 @@ import ( "strings" + "golang.org/x/net/context" + "net/url" "encoding/xml" @@ -271,6 +273,12 @@ func TestContextForm(t *testing.T) { assert.Equal(t, "joe@labstack.com", 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) diff --git a/website/content/guide/request.md b/website/content/guide/request.md index e747ca2e..dadba26b 100644 --- a/website/content/guide/request.md +++ b/website/content/guide/request.md @@ -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* @@ -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* @@ -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* @@ -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*