diff --git a/context.go b/context.go index f327adc7..df7cf076 100644 --- a/context.go +++ b/context.go @@ -47,7 +47,7 @@ type ( Redirect(int, string) error Error(err error) Logger() logger.Logger - Context() *context + Object() *context } context struct { @@ -307,8 +307,8 @@ func (c *context) Logger() logger.Logger { return c.echo.logger } -// Context returns the `context` instance. -func (c *context) Context() *context { +// Object returns the `context` object. +func (c *context) Object() *context { return c } diff --git a/context_test.go b/context_test.go index fd127864..6614286f 100644 --- a/context_test.go +++ b/context_test.go @@ -51,8 +51,8 @@ func TestContext(t *testing.T) { assert.Nil(t, c.Socket()) // Param by id - c.Context().pnames = []string{"id"} - c.Context().pvalues = []string{"1"} + c.Object().pnames = []string{"id"} + c.Object().pvalues = []string{"1"} assert.Equal(t, "1", c.P(0)) // Param by name @@ -68,13 +68,13 @@ func TestContext(t *testing.T) { // JSON testBindOk(t, c, ApplicationJSON) - c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent)) + c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent)) testBindError(t, c, ApplicationJSON) // XML - c.Context().request = test.NewRequest(POST, "/", strings.NewReader(userXML)) + c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML)) testBindOk(t, c, ApplicationXML) - c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent)) + c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent)) testBindError(t, c, ApplicationXML) // Unsupported @@ -87,14 +87,14 @@ func TestContext(t *testing.T) { tpl := &Template{ templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")), } - c.Context().echo.SetRenderer(tpl) + c.Object().echo.SetRenderer(tpl) err := c.Render(http.StatusOK, "hello", "Joe") if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Status()) assert.Equal(t, "Hello, Joe!", rec.Body.String()) } - c.Context().echo.renderer = nil + c.Object().echo.renderer = nil err = c.Render(http.StatusOK, "hello", "Joe") assert.Error(t, err) @@ -226,12 +226,12 @@ func TestContext(t *testing.T) { // Error rec = test.NewResponseRecorder() - c = NewContext(req, rec, e).Context() + c = NewContext(req, rec, e).Object() c.Error(errors.New("error")) assert.Equal(t, http.StatusInternalServerError, c.Response().Status()) // reset - c.Context().reset(req, test.NewResponseRecorder(), e) + c.Object().reset(req, test.NewResponseRecorder(), e) } func TestContextPath(t *testing.T) { diff --git a/engine/engine.go b/engine/engine.go index b058355a..a17c68ce 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -15,16 +15,17 @@ type ( } Request interface { + URI() string + URL() URL Header() Header // Proto() string // ProtoMajor() int // ProtoMinor() int RemoteAddress() string Method() string - URI() string - URL() URL Body() io.ReadCloser FormValue(string) string + Object() interface{} } Response interface { @@ -36,6 +37,7 @@ type ( Committed() bool SetWriter(io.Writer) Writer() io.Writer + Object() interface{} } Header interface { diff --git a/engine/fasthttp/request.go b/engine/fasthttp/request.go index d539e852..60c62cb1 100644 --- a/engine/fasthttp/request.go +++ b/engine/fasthttp/request.go @@ -9,30 +9,34 @@ import ( type ( Request struct { - context *fasthttp.RequestCtx + request *fasthttp.RequestCtx url engine.URL header engine.Header } ) +func (r *Request) Object() interface{} { + return r.request +} + +func (r *Request) URI() string { + return string(r.request.RequestURI()) +} + +func (r *Request) URL() engine.URL { + return r.url +} + func (r *Request) Header() engine.Header { return r.header } func (r *Request) RemoteAddress() string { - return r.context.RemoteAddr().String() + return r.request.RemoteAddr().String() } func (r *Request) Method() string { - return string(r.context.Method()) -} - -func (r *Request) URI() string { - return string(r.context.RequestURI()) -} - -func (r *Request) URL() engine.URL { - return r.url + return string(r.request.Method()) } func (r *Request) Body() io.ReadCloser { diff --git a/engine/fasthttp/response.go b/engine/fasthttp/response.go index b2f4c344..9f4a22c3 100644 --- a/engine/fasthttp/response.go +++ b/engine/fasthttp/response.go @@ -9,7 +9,7 @@ import ( type ( Response struct { - context *fasthttp.RequestCtx + response *fasthttp.RequestCtx header engine.Header status int size int64 @@ -18,16 +18,20 @@ type ( } ) +func (r *Response) Object() interface{} { + return r.response +} + func (r *Response) Header() engine.Header { return r.header } func (r *Response) WriteHeader(code int) { - r.context.SetStatusCode(code) + r.response.SetStatusCode(code) } func (r *Response) Write(b []byte) (int, error) { - return r.context.Write(b) + return r.response.Write(b) } func (r *Response) Status() int { diff --git a/engine/fasthttp/server.go b/engine/fasthttp/server.go index 1f2ee558..222d6fcd 100644 --- a/engine/fasthttp/server.go +++ b/engine/fasthttp/server.go @@ -29,13 +29,13 @@ func NewServer(c *engine.Config, h engine.HandlerFunc, l logger.Logger) *Server func (s *Server) Start() { fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) { req := &Request{ - context: ctx, + request: ctx, url: &URL{ctx.URI()}, header: &RequestHeader{ctx.Request.Header}, } res := &Response{ - context: ctx, - header: &ResponseHeader{ctx.Response.Header}, + response: ctx, + header: &ResponseHeader{ctx.Response.Header}, } s.handler(req, res) }) diff --git a/engine/standard/request.go b/engine/standard/request.go index eabb3d12..32c224bb 100644 --- a/engine/standard/request.go +++ b/engine/standard/request.go @@ -23,18 +23,18 @@ func NewRequest(r *http.Request) *Request { } } -func (r *Request) Request() *http.Request { +func (r *Request) Object() interface{} { return r.request } -func (r *Request) Header() engine.Header { - return r.header -} - func (r *Request) URL() engine.URL { return r.url } +func (r *Request) Header() engine.Header { + return r.header +} + func (r *Request) RemoteAddress() string { return r.request.RemoteAddr } diff --git a/engine/standard/response.go b/engine/standard/response.go index d0de29ab..8efb796f 100644 --- a/engine/standard/response.go +++ b/engine/standard/response.go @@ -29,6 +29,10 @@ func NewResponse(w http.ResponseWriter, l logger.Logger) *Response { } } +func (r *Response) Object() interface{} { + return r.response +} + func (r *Response) Header() engine.Header { return r.header } diff --git a/router.go b/router.go index d355b5b3..ae0394e6 100644 --- a/router.go +++ b/router.go @@ -274,7 +274,7 @@ func (n *node) check405() HandlerFunc { } func (r *Router) Find(method, path string, context Context) (h HandlerFunc, e *Echo) { - x := context.Context() + x := context.Object() h = notFoundHandler e = r.echo cn := r.tree // Current node as root diff --git a/router_test.go b/router_test.go index 5da88a20..d228ebd6 100644 --- a/router_test.go +++ b/router_test.go @@ -529,16 +529,16 @@ func TestRouterParamNames(t *testing.T) { // Route > /users/:id h, _ = r.Find(GET, "/users/1", c) if assert.NotNil(t, h) { - assert.Equal(t, "id", c.Context().pnames[0]) + assert.Equal(t, "id", c.Object().pnames[0]) assert.Equal(t, "1", c.P(0)) } // Route > /users/:uid/files/:fid h, _ = r.Find(GET, "/users/1/files/1", c) if assert.NotNil(t, h) { - assert.Equal(t, "uid", c.Context().pnames[0]) + assert.Equal(t, "uid", c.Object().pnames[0]) assert.Equal(t, "1", c.P(0)) - assert.Equal(t, "fid", c.Context().pnames[1]) + assert.Equal(t, "fid", c.Object().pnames[1]) assert.Equal(t, "1", c.P(1)) } } @@ -556,7 +556,7 @@ func TestRouterAPI(t *testing.T) { for _, route := range api { h, _ := r.Find(route.Method, route.Path, c) if assert.NotNil(t, h) { - for i, n := range c.Context().pnames { + for i, n := range c.Object().pnames { if assert.NotEmpty(t, n) { assert.Equal(t, ":"+n, c.P(i)) }