We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Invoke some callbacks before Write Header (for middleware)
// XRuntime Middleware func XRuntime(next echo.HandlerFunc) echo.HandlerFunc { return func(ctx echo.Context) (err error) { start := time.Now() ctx.Response().OnBeforeWriteHeader(func(resp *echo.Response) { resp.Header().Set("x-runtime", time.Since(start).String()) }) return next(ctx) } } e := echo.New() api := e.Group("/api", XRuntime)
Response struct { Writer http.ResponseWriter Status int Size int64 Committed bool echo *Echo writeHeaderCallbacks []func(resp *Response) } func (r *Response) OnBeforeWriteHeader(callback func(resp *Response)) { r.writeHeaderCallbacks = append(r.writeHeaderCallbacks, callback) } func (r *Response) WriteHeader(code int) { if r.Committed { r.echo.Logger.Warn("response already committed") return } r.Status = code for _, callback := range r.writeHeaderCallbacks { callback(r) } r.Writer.WriteHeader(code) r.Committed = true }
Please confirm echo to achieve similar functionality, what is the best practice?
The text was updated successfully, but these errors were encountered:
May be related #176
Sorry, something went wrong.
f96c973
vishr
No branches or pull requests
Invoke some callbacks before Write Header (for middleware)
Please confirm echo to achieve similar functionality, what is the best practice?
The text was updated successfully, but these errors were encountered: