Skip to content

Commit

Permalink
Changed signature for response before func
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jun 29, 2017
1 parent c7531df commit ee85b46
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 2 additions & 4 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 5 additions & 9 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit ee85b46

Please sign in to comment.