diff --git a/echo.go b/echo.go index 94e2ab10..63e67152 100644 --- a/echo.go +++ b/echo.go @@ -293,16 +293,14 @@ func New() (e *Echo) { // NewContext returns a Context instance. func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context { - c := &context{ + return &context{ request: r, - response: &Response{Writer: w}, + response: &Response{echo: e, Writer: w}, store: make(Map), echo: e, pvalues: make([]string, *e.maxParam), handler: NotFoundHandler, } - c.response.context = c - return c } // Router returns router. diff --git a/response.go b/response.go index 673f6db6..68b99155 100644 --- a/response.go +++ b/response.go @@ -11,17 +11,13 @@ type ( // by an HTTP handler to construct an HTTP response. // See: https://golang.org/pkg/net/http/#ResponseWriter Response struct { - context Context - beforeFuncs []BeforeResponseFunc + echo *Echo + beforeFuncs []func() Writer http.ResponseWriter Status int Size int64 Committed bool } - - // BeforeResponseFunc defines a function which is called just before writing the - // response. - BeforeResponseFunc func(Context) ) // Header returns the header map for the writer that will be sent by @@ -35,7 +31,7 @@ func (r *Response) Header() http.Header { } // Before registers a function which is called just before the response is written. -func (r *Response) Before(fn BeforeResponseFunc) { +func (r *Response) Before(fn func()) { r.beforeFuncs = append(r.beforeFuncs, fn) } @@ -45,11 +41,11 @@ func (r *Response) Before(fn BeforeResponseFunc) { // used to send error codes. func (r *Response) WriteHeader(code int) { if r.Committed { - r.context.Logger().Warn("response already committed") + r.echo.Logger.Warn("response already committed") return } for _, fn := range r.beforeFuncs { - fn(r.context) + fn() } r.Status = code r.Writer.WriteHeader(code) diff --git a/response_test.go b/response_test.go index 95fb3227..21ac072c 100644 --- a/response_test.go +++ b/response_test.go @@ -12,10 +12,10 @@ func TestResponse(t *testing.T) { req := httptest.NewRequest(GET, "/", nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - res := &Response{context: c, Writer: rec} + res := &Response{echo: e, Writer: rec} // Before - res.Before(func(c Context) { + res.Before(func() { c.Response().Header().Set(HeaderServer, "echo") }) res.Write([]byte("test"))