mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Enhanced recover middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
0465314380
commit
00bf0d651f
@ -7,7 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
BasicAuthOptions struct {
|
||||
BasicAuthConfig struct {
|
||||
AuthFunc BasicAuthFunc
|
||||
}
|
||||
|
||||
BasicAuthFunc func(string, string) bool
|
||||
@ -17,11 +18,21 @@ const (
|
||||
basic = "Basic"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultBasicAuthConfig = BasicAuthConfig{}
|
||||
)
|
||||
|
||||
// BasicAuth returns an HTTP basic authentication middleware.
|
||||
//
|
||||
// For valid credentials it calls the next handler.
|
||||
// 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 echo.HandlerFunc(func(c echo.Context) error {
|
||||
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++ {
|
||||
if cred[i] == ':' {
|
||||
// Verify credentials
|
||||
if fn(cred[:i], cred[i+1:]) {
|
||||
if config.AuthFunc(cred[:i], cred[i+1:]) {
|
||||
return next.Handle(c)
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
GzipOptions struct {
|
||||
level int
|
||||
GzipConfig struct {
|
||||
Level int
|
||||
}
|
||||
|
||||
gzipResponseWriter struct {
|
||||
@ -23,11 +23,24 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
defaultGzipConfig = GzipConfig{
|
||||
Level: gzip.DefaultCompression,
|
||||
}
|
||||
)
|
||||
|
||||
// Gzip returns a middleware which compresses HTTP response using gzip compression
|
||||
// scheme.
|
||||
func Gzip(options ...GzipOptions) echo.MiddlewareFunc {
|
||||
func Gzip() echo.MiddlewareFunc {
|
||||
return GzipFromConfig(defaultGzipConfig)
|
||||
}
|
||||
|
||||
// GzipFromConfig return `Gzip` middleware from config.
|
||||
func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
||||
pool := gzipPool(config)
|
||||
scheme := "gzip"
|
||||
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
scheme := "gzip"
|
||||
return echo.HandlerFunc(func(c echo.Context) error {
|
||||
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
||||
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)
|
||||
}
|
||||
|
||||
var pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return gzip.NewWriter(ioutil.Discard)
|
||||
},
|
||||
func gzipPool(config GzipConfig) sync.Pool {
|
||||
return sync.Pool{
|
||||
New: func() interface{} {
|
||||
w, _ := gzip.NewWriterLevel(ioutil.Discard, config.Level)
|
||||
return w
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -9,11 +9,19 @@ import (
|
||||
)
|
||||
|
||||
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 echo.HandlerFunc(func(c echo.Context) error {
|
||||
req := c.Request()
|
||||
|
@ -1,36 +1,53 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/gommon/color"
|
||||
)
|
||||
|
||||
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
|
||||
// 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 {
|
||||
// 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 r := recover(); r != nil {
|
||||
e := ""
|
||||
var err error
|
||||
switch r := r.(type) {
|
||||
case string:
|
||||
e = r
|
||||
case error:
|
||||
e = r.Error()
|
||||
err = r
|
||||
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)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
@ -12,6 +13,8 @@ import (
|
||||
func TestRecover(t *testing.T) {
|
||||
e := echo.New()
|
||||
e.SetDebug(true)
|
||||
buf := new(bytes.Buffer)
|
||||
e.SetLogOutput(buf)
|
||||
req := test.NewRequest(echo.GET, "/", nil)
|
||||
rec := test.NewResponseRecorder()
|
||||
c := echo.NewContext(req, rec, e)
|
||||
@ -20,5 +23,5 @@ func TestRecover(t *testing.T) {
|
||||
}))
|
||||
h.Handle(c)
|
||||
assert.Equal(t, http.StatusInternalServerError, rec.Status())
|
||||
assert.Contains(t, rec.Body.String(), "panic recover")
|
||||
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
||||
}
|
||||
|
@ -9,26 +9,30 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
StaticOptions struct {
|
||||
StaticConfig struct {
|
||||
Root string `json:"root"`
|
||||
Index string `json:"index"`
|
||||
Browse bool `json:"browse"`
|
||||
}
|
||||
)
|
||||
|
||||
func Static(root string, options ...StaticOptions) echo.MiddlewareFunc {
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
// Default options
|
||||
opts := StaticOptions{}
|
||||
if len(options) > 0 {
|
||||
opts = options[0]
|
||||
}
|
||||
if opts.Index == "" {
|
||||
opts.Index = "index.html"
|
||||
}
|
||||
var (
|
||||
DefaultStaticConfig = StaticConfig{
|
||||
Index: "index.html",
|
||||
Browse: false,
|
||||
}
|
||||
)
|
||||
|
||||
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 {
|
||||
fs := http.Dir(root)
|
||||
fs := http.Dir(config.Root)
|
||||
file := path.Clean(c.Request().URL().Path())
|
||||
f, err := fs.Open(file)
|
||||
if err != nil {
|
||||
@ -49,10 +53,10 @@ func Static(root string, options ...StaticOptions) echo.MiddlewareFunc {
|
||||
d := f
|
||||
|
||||
// Index file
|
||||
file = path.Join(file, opts.Index)
|
||||
file = path.Join(file, config.Index)
|
||||
f, err = fs.Open(file)
|
||||
if err != nil {
|
||||
if opts.Browse {
|
||||
if config.Browse {
|
||||
dirs, err := d.Readdir(-1)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user