1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Fixed recover and gzip middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-10 16:35:20 -08:00
parent 76cfd2f666
commit 25e72d623e
9 changed files with 49 additions and 52 deletions

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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
}

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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