1
0
mirror of https://github.com/labstack/echo.git synced 2025-05-13 22:06:36 +02:00

Enhanced recover middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-14 13:55:38 -07:00
parent 0465314380
commit 00bf0d651f
6 changed files with 99 additions and 40 deletions

View File

@ -7,7 +7,8 @@ import (
) )
type ( type (
BasicAuthOptions struct { BasicAuthConfig struct {
AuthFunc BasicAuthFunc
} }
BasicAuthFunc func(string, string) bool BasicAuthFunc func(string, string) bool
@ -17,11 +18,21 @@ const (
basic = "Basic" basic = "Basic"
) )
var (
DefaultBasicAuthConfig = BasicAuthConfig{}
)
// BasicAuth returns an HTTP basic authentication middleware. // BasicAuth returns an HTTP basic authentication middleware.
// //
// For valid credentials it calls the next handler. // For valid credentials it calls the next handler.
// For invalid credentials, it sends "401 - Unauthorized" response. // For invalid credentials, it sends "401 - Unauthorized" response.
func BasicAuth(fn BasicAuthFunc, options ...BasicAuthOptions) echo.MiddlewareFunc { func BasicAuth(fn BasicAuthFunc) echo.MiddlewareFunc {
c := DefaultBasicAuthConfig
c.AuthFunc = fn
return BasicAuthFromConfig(c)
}
func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler { return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error { return echo.HandlerFunc(func(c echo.Context) error {
auth := c.Request().Header().Get(echo.Authorization) auth := c.Request().Header().Get(echo.Authorization)
@ -34,7 +45,7 @@ func BasicAuth(fn BasicAuthFunc, options ...BasicAuthOptions) echo.MiddlewareFun
for i := 0; i < len(cred); i++ { for i := 0; i < len(cred); i++ {
if cred[i] == ':' { if cred[i] == ':' {
// Verify credentials // Verify credentials
if fn(cred[:i], cred[i+1:]) { if config.AuthFunc(cred[:i], cred[i+1:]) {
return next.Handle(c) return next.Handle(c)
} }
} }

View File

@ -13,8 +13,8 @@ import (
) )
type ( type (
GzipOptions struct { GzipConfig struct {
level int Level int
} }
gzipResponseWriter struct { gzipResponseWriter struct {
@ -23,11 +23,24 @@ type (
} }
) )
var (
defaultGzipConfig = GzipConfig{
Level: gzip.DefaultCompression,
}
)
// Gzip returns a middleware which compresses HTTP response using gzip compression // Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme. // scheme.
func Gzip(options ...GzipOptions) echo.MiddlewareFunc { func Gzip() echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler { return GzipFromConfig(defaultGzipConfig)
}
// GzipFromConfig return `Gzip` middleware from config.
func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
pool := gzipPool(config)
scheme := "gzip" scheme := "gzip"
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error { return echo.HandlerFunc(func(c echo.Context) error {
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding) c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) { if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
@ -56,8 +69,11 @@ func (g gzipResponseWriter) Write(b []byte) (int, error) {
return g.Writer.Write(b) return g.Writer.Write(b)
} }
var pool = sync.Pool{ func gzipPool(config GzipConfig) sync.Pool {
return sync.Pool{
New: func() interface{} { New: func() interface{} {
return gzip.NewWriter(ioutil.Discard) w, _ := gzip.NewWriterLevel(ioutil.Discard, config.Level)
return w
}, },
}
} }

View File

@ -9,11 +9,19 @@ import (
) )
type ( type (
LoggerOptions struct { LoggerConfig struct {
} }
) )
func Logger(options ...LoggerOptions) echo.MiddlewareFunc { var (
DefaultLoggerConfig = LoggerConfig{}
)
func Logger() echo.MiddlewareFunc {
return LoggerFromConfig(DefaultLoggerConfig)
}
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler { return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error { return echo.HandlerFunc(func(c echo.Context) error {
req := c.Request() req := c.Request()

View File

@ -1,36 +1,53 @@
package middleware package middleware
import ( import (
"errors" "fmt"
"runtime"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/gommon/color"
) )
type ( type (
RecoverOptions struct { RecoverConfig struct {
StackSize int
StackAll bool
PrintStack bool
} }
) )
var (
DefaultRecoverConfig = RecoverConfig{
StackSize: 4 << 10, // 4 KB
StackAll: true,
PrintStack: true,
}
)
func Recover() echo.MiddlewareFunc {
return RecoverWithConfig(DefaultRecoverConfig)
}
// Recover returns a middleware which recovers from panics anywhere in the chain // Recover returns a middleware which recovers from panics anywhere in the chain
// and handles the control to the centralized HTTPErrorHandler. // and handles the control to the centralized HTTPErrorHandler.
func Recover(options ...RecoverOptions) echo.MiddlewareFunc { func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler { 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`
return echo.HandlerFunc(func(c echo.Context) error { return echo.HandlerFunc(func(c echo.Context) error {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
e := "" var err error
switch r := r.(type) { switch r := r.(type) {
case string:
e = r
case error: case error:
e = r.Error() err = r
default: default:
e = "unknown error" err = fmt.Errorf("%v", r)
} }
c.Error(errors.New("panic recover|" + e)) stack := make([]byte, config.StackSize)
length := runtime.Stack(stack, config.StackAll)
if config.PrintStack {
c.Logger().Printf("%s|%s", color.Red("PANIC RECOVER"), stack[:length])
}
c.Error(err)
} }
}() }()
return next.Handle(c) return next.Handle(c)

View File

@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"bytes"
"net/http" "net/http"
"testing" "testing"
@ -12,6 +13,8 @@ import (
func TestRecover(t *testing.T) { func TestRecover(t *testing.T) {
e := echo.New() e := echo.New()
e.SetDebug(true) e.SetDebug(true)
buf := new(bytes.Buffer)
e.SetLogOutput(buf)
req := test.NewRequest(echo.GET, "/", nil) req := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder() rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e) c := echo.NewContext(req, rec, e)
@ -20,5 +23,5 @@ func TestRecover(t *testing.T) {
})) }))
h.Handle(c) h.Handle(c)
assert.Equal(t, http.StatusInternalServerError, rec.Status()) assert.Equal(t, http.StatusInternalServerError, rec.Status())
assert.Contains(t, rec.Body.String(), "panic recover") assert.Contains(t, buf.String(), "PANIC RECOVER")
} }

View File

@ -9,26 +9,30 @@ import (
) )
type ( type (
StaticOptions struct { StaticConfig struct {
Root string `json:"root"` Root string `json:"root"`
Index string `json:"index"` Index string `json:"index"`
Browse bool `json:"browse"` Browse bool `json:"browse"`
} }
) )
func Static(root string, options ...StaticOptions) echo.MiddlewareFunc { var (
return func(next echo.Handler) echo.Handler { DefaultStaticConfig = StaticConfig{
// Default options Index: "index.html",
opts := StaticOptions{} Browse: false,
if len(options) > 0 {
opts = options[0]
}
if opts.Index == "" {
opts.Index = "index.html"
} }
)
func Static(root string) echo.MiddlewareFunc {
c := DefaultStaticConfig
c.Root = root
return StaticFromConfig(c)
}
func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error { return echo.HandlerFunc(func(c echo.Context) error {
fs := http.Dir(root) fs := http.Dir(config.Root)
file := path.Clean(c.Request().URL().Path()) file := path.Clean(c.Request().URL().Path())
f, err := fs.Open(file) f, err := fs.Open(file)
if err != nil { if err != nil {
@ -49,10 +53,10 @@ func Static(root string, options ...StaticOptions) echo.MiddlewareFunc {
d := f d := f
// Index file // Index file
file = path.Join(file, opts.Index) file = path.Join(file, config.Index)
f, err = fs.Open(file) f, err = fs.Open(file)
if err != nil { if err != nil {
if opts.Browse { if config.Browse {
dirs, err := d.Readdir(-1) dirs, err := d.Readdir(-1)
if err != nil { if err != nil {
return err return err