1
0
mirror of https://github.com/labstack/echo.git synced 2025-05-31 23:19:42 +02:00

Changed signature for response before func

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-06-29 10:59:21 -07:00
parent c7531df815
commit ee85b46a05
3 changed files with 9 additions and 15 deletions

View File

@ -293,16 +293,14 @@ func New() (e *Echo) {
// NewContext returns a Context instance. // NewContext returns a Context instance.
func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context { func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context {
c := &context{ return &context{
request: r, request: r,
response: &Response{Writer: w}, response: &Response{echo: e, Writer: w},
store: make(Map), store: make(Map),
echo: e, echo: e,
pvalues: make([]string, *e.maxParam), pvalues: make([]string, *e.maxParam),
handler: NotFoundHandler, handler: NotFoundHandler,
} }
c.response.context = c
return c
} }
// Router returns router. // Router returns router.

View File

@ -11,17 +11,13 @@ type (
// by an HTTP handler to construct an HTTP response. // by an HTTP handler to construct an HTTP response.
// See: https://golang.org/pkg/net/http/#ResponseWriter // See: https://golang.org/pkg/net/http/#ResponseWriter
Response struct { Response struct {
context Context echo *Echo
beforeFuncs []BeforeResponseFunc beforeFuncs []func()
Writer http.ResponseWriter Writer http.ResponseWriter
Status int Status int
Size int64 Size int64
Committed bool 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 // 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. // 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) r.beforeFuncs = append(r.beforeFuncs, fn)
} }
@ -45,11 +41,11 @@ func (r *Response) Before(fn BeforeResponseFunc) {
// used to send error codes. // used to send error codes.
func (r *Response) WriteHeader(code int) { func (r *Response) WriteHeader(code int) {
if r.Committed { if r.Committed {
r.context.Logger().Warn("response already committed") r.echo.Logger.Warn("response already committed")
return return
} }
for _, fn := range r.beforeFuncs { for _, fn := range r.beforeFuncs {
fn(r.context) fn()
} }
r.Status = code r.Status = code
r.Writer.WriteHeader(code) r.Writer.WriteHeader(code)

View File

@ -12,10 +12,10 @@ func TestResponse(t *testing.T) {
req := httptest.NewRequest(GET, "/", nil) req := httptest.NewRequest(GET, "/", nil)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
c := e.NewContext(req, rec) c := e.NewContext(req, rec)
res := &Response{context: c, Writer: rec} res := &Response{echo: e, Writer: rec}
// Before // Before
res.Before(func(c Context) { res.Before(func() {
c.Response().Header().Set(HeaderServer, "echo") c.Response().Header().Set(HeaderServer, "echo")
}) })
res.Write([]byte("test")) res.Write([]byte("test"))