Skip to content

add custom context support #1355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ type (
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
// See `Echo#ServeHTTP()`
Reset(r *http.Request, w http.ResponseWriter)

// Underlying returns the underlying context.
Underlying() Context

// release managed resources
Free()
}

context struct {
Expand Down Expand Up @@ -604,3 +610,9 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
// c.pvalues = nil
}

func (c *context) Underlying() Context {
return nil
}

func (c *context) Free() {}
15 changes: 14 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type (
routers map[string]*Router
notFoundHandler HandlerFunc
pool sync.Pool
newCtx func(r *http.Request, w http.ResponseWriter) Context
Server *http.Server
TLSServer *http.Server
Listener net.Listener
Expand Down Expand Up @@ -315,8 +316,19 @@ func New() (e *Echo) {
return
}

func (e *Echo) SetNewContext(newCtx func(r *http.Request, w http.ResponseWriter) Context) {
e.newCtx = newCtx
}

// NewContext returns a Context instance.
func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context {
if e.newCtx != nil {
return e.newCtx(r, w)
}
return e.NewNativeContext(r, w)
}

func (e *Echo) NewNativeContext(r *http.Request, w http.ResponseWriter) Context {
return &context{
request: r,
response: NewResponse(w, e),
Expand Down Expand Up @@ -587,13 +599,14 @@ func (e *Echo) AcquireContext() Context {
// ReleaseContext returns the `Context` instance back to the pool.
// You must call it after `AcquireContext()`.
func (e *Echo) ReleaseContext(c Context) {
c.Free()
e.pool.Put(c)
}

// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Acquire context
c := e.pool.Get().(*context)
c := e.pool.Get().(Context)
c.Reset(r, w)

h := NotFoundHandler
Expand Down
51 changes: 51 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,54 @@ func TestEchoShutdown(t *testing.T) {
err := <-errCh
assert.Equal(t, err.Error(), "http: Server closed")
}

type MyContext struct {
Context

MyField1 int
MyField2 int
MyField3 int
}

func (ctx *MyContext) Underlying() Context {
return ctx.Context
}

func (ctx *MyContext) Free() {
ctx.MyField1 = 0
ctx.MyField2 = 0
ctx.MyField3 = 0
}

func TestEchoWrapContext(t *testing.T) {
e := New()
e.SetNewContext(func(r *http.Request, w http.ResponseWriter) Context {
return &MyContext{Context: e.NewNativeContext(r, w)}
})
toEchoFunc := func(h func(ctx *MyContext) error) HandlerFunc {
return func(ctx Context) error {
return h(ctx.(*MyContext))
}
}

e.Pre(func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
ctx := c.(*MyContext)
ctx.MyField1 = 1
ctx.MyField2 = 2
ctx.MyField3 = 3
return next(c)
}
})

e.GET("/users", toEchoFunc(func(c *MyContext) error {
assert.Equal(t, c.MyField1, 1)
assert.Equal(t, c.MyField2, 2)
assert.Equal(t, c.MyField3, 3)
return c.String(http.StatusOK, "ok")
}))

c, b := request(http.MethodGet, "/users", e)
assert.Equal(t, http.StatusOK, c)
assert.Equal(t, "ok", b)
}
21 changes: 19 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package echo

import "net/http"
import (
"errors"
"net/http"
)

type (
// Router is the registry of all registered routes for an `Echo` instance for
Expand Down Expand Up @@ -300,7 +303,21 @@ func (n *node) checkMethodNotAllowed() HandlerFunc {
// - Reset it `Context#Reset()`
// - Return it `Echo#ReleaseContext()`.
func (r *Router) Find(method, path string, c Context) {
ctx := c.(*context)
ctx, isNativeCtx := c.(*context)
if !isNativeCtx {
for {
c = c.Underlying()
if c == nil {
panic(errors.New("must has underlying native context"))
}

ctx, isNativeCtx = c.(*context)
if isNativeCtx {
break
}
}
}

ctx.path = path
cn := r.tree // Current node as root

Expand Down