From 25e72d623e44806303a8691b1240215d6ee3853d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 10 Mar 2016 16:35:20 -0800 Subject: [PATCH] Fixed recover and gzip middleware Signed-off-by: Vishal Rana --- context.go | 6 ------ echo.go | 4 ++-- engine/engine.go | 4 ++-- engine/fasthttp/response.go | 14 +++++++------- engine/standard/response.go | 25 +++++++++++++------------ engine/standard/server.go | 16 ++++++---------- middleware/compress.go | 6 +++--- middleware/recover.go | 24 +++++++++++++++--------- website/content/guide/customization.md | 2 +- 9 files changed, 49 insertions(+), 52 deletions(-) diff --git a/context.go b/context.go index 7355df3d..d5888bac 100644 --- a/context.go +++ b/context.go @@ -26,7 +26,6 @@ type ( Context interface { netContext.Context Request() engine.Request - SetResponse(engine.Response) Response() engine.Response Path() string P(int) string @@ -111,11 +110,6 @@ func (c *context) Request() engine.Request { return c.request } -// SetResponse sets `engine.Response`. -func (c *context) SetResponse(r engine.Response) { - c.response = r -} - // Response returns `engine.Response`. func (c *context) Response() engine.Response { return c.response diff --git a/echo.go b/echo.go index 8b1af536..bef1c1e3 100644 --- a/echo.go +++ b/echo.go @@ -197,7 +197,7 @@ func New() (e *Echo) { // Logger e.logger = log.New("echo") - e.logger.SetLevel(log.FATAL) + e.logger.SetLevel(log.ERROR) return } @@ -249,7 +249,7 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) { if !c.Response().Committed() { c.String(code, msg) } - e.logger.Debug(err) + e.logger.Error(err) } // SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler. diff --git a/engine/engine.go b/engine/engine.go index f14c1cad..f94fda04 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -40,8 +40,8 @@ type ( Status() int Size() int64 Committed() bool - // SetWriter(io.Writer) - // Writer() io.Writer + SetWriter(io.Writer) + Writer() io.Writer } // Header defines an interface for HTTP header. diff --git a/engine/fasthttp/response.go b/engine/fasthttp/response.go index 51669f55..e394193b 100644 --- a/engine/fasthttp/response.go +++ b/engine/fasthttp/response.go @@ -62,13 +62,13 @@ func (r *Response) Committed() bool { return r.committed } -// func (r *Response) SetWriter(w io.Writer) { -// r.writer = w -// } -// -// func (r *Response) Writer() io.Writer { -// return r.writer -// } +func (r *Response) SetWriter(w io.Writer) { + r.writer = w +} + +func (r *Response) Writer() io.Writer { + return r.writer +} func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) { r.RequestCtx = c diff --git a/engine/standard/response.go b/engine/standard/response.go index 073fd781..5c195a4d 100644 --- a/engine/standard/response.go +++ b/engine/standard/response.go @@ -1,6 +1,7 @@ package standard import ( + "io" "net/http" "github.com/labstack/echo/engine" @@ -14,8 +15,8 @@ type ( status int size int64 committed bool - // writer io.Writer - logger *log.Logger + writer io.Writer + logger *log.Logger } ) @@ -23,8 +24,8 @@ func NewResponse(w http.ResponseWriter, l *log.Logger) *Response { return &Response{ ResponseWriter: w, header: &Header{w.Header()}, - // writer: w, - logger: l, + writer: w, + logger: l, } } @@ -43,7 +44,7 @@ func (r *Response) WriteHeader(code int) { } func (r *Response) Write(b []byte) (n int, err error) { - n, err = r.ResponseWriter.Write(b) + n, err = r.writer.Write(b) r.size += int64(n) return } @@ -60,13 +61,13 @@ func (r *Response) Committed() bool { return r.committed } -// func (r *Response) SetWriter(w io.Writer) { -// r.writer = w -// } +func (r *Response) SetWriter(w io.Writer) { + r.writer = w +} -// func (r *Response) Writer() io.Writer { -// return r.writer -// } +func (r *Response) Writer() io.Writer { + return r.writer +} func (r *Response) reset(w http.ResponseWriter, h engine.Header) { r.ResponseWriter = w @@ -74,5 +75,5 @@ func (r *Response) reset(w http.ResponseWriter, h engine.Header) { r.status = http.StatusOK r.size = 0 r.committed = false - // r.writer = w + r.writer = w } diff --git a/engine/standard/server.go b/engine/standard/server.go index b34eaf87..d4b744b6 100644 --- a/engine/standard/server.go +++ b/engine/standard/server.go @@ -11,7 +11,7 @@ import ( type ( Server struct { - server *http.Server + *http.Server config *engine.Config handler engine.Handler logger *log.Logger @@ -42,7 +42,7 @@ func NewFromTLS(addr, certfile, keyfile string) *Server { func NewFromConfig(c *engine.Config) (s *Server) { s = &Server{ - server: new(http.Server), + Server: new(http.Server), config: c, pool: &Pool{ request: sync.Pool{ @@ -71,8 +71,8 @@ func NewFromConfig(c *engine.Config) (s *Server) { }), logger: log.New("echo"), } - s.server.Addr = c.Address - s.server.Handler = s + s.Addr = c.Address + s.Handler = s return } @@ -88,9 +88,9 @@ func (s *Server) Start() { certfile := s.config.TLSCertfile keyfile := s.config.TLSKeyfile if certfile != "" && keyfile != "" { - s.logger.Fatal(s.server.ListenAndServeTLS(certfile, keyfile)) + s.logger.Fatal(s.ListenAndServeTLS(certfile, keyfile)) } else { - s.logger.Fatal(s.server.ListenAndServe()) + s.logger.Fatal(s.ListenAndServe()) } } @@ -118,10 +118,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.pool.header.Put(resHdr) } -func (s *Server) Server() *http.Server { - return s.server -} - // WrapHandler wraps `http.Handler` into `echo.HandlerFunc`. func WrapHandler(h http.Handler) echo.HandlerFunc { return func(c echo.Context) error { diff --git a/middleware/compress.go b/middleware/compress.go index 8a4112d4..672a74f3 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -32,14 +32,14 @@ func Gzip(options ...*GzipOptions) echo.MiddlewareFunc { c.Response().Header().Add(echo.Vary, echo.AcceptEncoding) if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) { w := pool.Get().(*gzip.Writer) - w.Reset(c.Response()) + w.Reset(c.Response().Writer()) defer func() { w.Close() pool.Put(w) }() - gw := gzipResponseWriter{Response: c.Response(), Writer: w} + g := gzipResponseWriter{Response: c.Response(), Writer: w} c.Response().Header().Set(echo.ContentEncoding, scheme) - c.SetResponse(gw) + c.Response().SetWriter(g) } if err := next.Handle(c); err != nil { c.Error(err) diff --git a/middleware/recover.go b/middleware/recover.go index 04f00212..6eabedcc 100644 --- a/middleware/recover.go +++ b/middleware/recover.go @@ -1,9 +1,7 @@ package middleware import ( - "fmt" - - "runtime" + "errors" "github.com/labstack/echo" ) @@ -17,14 +15,22 @@ type ( // and handles the control to the centralized HTTPErrorHandler. func Recover(options ...*RecoverOptions) echo.MiddlewareFunc { return func(next echo.Handler) echo.Handler { - // TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace` + // TODO: Provide better stack trace + // - `https://github.com/go-errors/errors` + // - `https://github.com/docker/libcontainer/tree/master/stacktrace` return echo.HandlerFunc(func(c echo.Context) error { defer func() { - if err := recover(); err != nil { - trace := make([]byte, 1<<16) - n := runtime.Stack(trace, true) - c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s", - err, n, trace[:n])) + if r := recover(); r != nil { + e := "" + switch r := r.(type) { + case string: + e = r + case error: + e = r.Error() + default: + e = "unknown error" + } + c.Error(errors.New("panic recover|" + e)) } }() return next.Handle(c) diff --git a/website/content/guide/customization.md b/website/content/guide/customization.md index 81a2f946..da466ef2 100644 --- a/website/content/guide/customization.md +++ b/website/content/guide/customization.md @@ -41,7 +41,7 @@ SetLogOutput sets the output destination for the logger. Default value is `os.St `echo#SetLogLevel(l log.Level)` -SetLogLevel sets the log level for the logger. Default value is `log.FATAL`. +SetLogLevel sets the log level for the logger. Default value is `log.ERROR`. ### Auto index