Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: add before hook in echo.Response? #176

Closed
cgyy opened this issue Aug 3, 2015 · 1 comment · Fixed by open-telemetry/opentelemetry-go-contrib#617 · May be fixed by evantorrie/opentelemetry-go-contrib#648
Closed
Assignees

Comments

@cgyy
Copy link

cgyy commented Aug 3, 2015

Before allows for a function to be called before the ResponseWriter has been written to. This is
useful for setting headers or any other operations that must happen before a response has been written

type (
    Response struct {
        writer    http.ResponseWriter
        status    int
        size      int64
        committed bool
        beforeFuncs []BeforeFunc
    }
)
type BeforeFunc func(ResponseWriter)


func (r *Response) Before(before HandlerFunc) {
    r.beforeFuncs = append(r.beforeFuncs, before)
}


func (r *Response) WriteHeader(code int) {
    if r.committed {
        // TODO: Warning
        log.Printf("echo => %s", color.Yellow("response already committed"))
        return
    }
    for i := len(r.beforeFuncs) - 1; i >= 0; i-- {
        r.beforeFuncs[i](r)
    }

    r.status = code
    r.writer.WriteHeader(code)
    r.committed = true
}

For, example, If want to save session in middleware, could write code like this:

func Sessions(name string, store Store) echo.MiddlewareFunc {
    return func(h echo.HandlerFunc) echo.HandlerFunc {
        return func(c *echo.Context) error {
            s := &session{name, c.Request(), store, nil, false, c.Response()}
            c.Set(DefaultKey, s)

            c.Response.Before(func(echo.Response) {
                if s.Written() {
                    s.Session().Save(s.request, s.writer)
                }
            })

            // and no need to define another response writer like this
            //sw := SessionWriter{s, c.Response().Writer()}
            //c.Response().SetWriter(&sw)

            defer context.Clear(c.Request())
            return h(c)
        }
    }
}   
@cgyy cgyy changed the title Could add before hook in echo.Response? Suggestion: add before hook in echo.Response? Aug 3, 2015
@ansel1
Copy link

ansel1 commented May 25, 2016

I've also got a use case like this. Just before headers are written, we calculate a total request handling time, and put that into a header in the response, so clients can calculate network latency.

We do it now by hijacking the ResponseWriter in middleware outside the echo.Router.

@vishr vishr self-assigned this May 25, 2016
@vishr vishr closed this as completed in f96c973 Jun 29, 2017
This was referenced Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants