mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Changed signature for response before func
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
c7531df815
commit
ee85b46a05
6
echo.go
6
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.
|
||||
|
14
response.go
14
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)
|
||||
|
@ -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"))
|
||||
|
Loading…
Reference in New Issue
Block a user