1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Middleware as structs

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-02-17 21:01:47 -08:00 committed by Vishal Rana
parent e91717552f
commit c9a62e20b4
9 changed files with 187 additions and 120 deletions

View File

@ -11,9 +11,9 @@ import (
type (
Static struct {
Root string
Index string
Browse bool
Root string `json:"root"`
Index string `json:"index"`
Browse bool `json:"browse"`
}
)

View File

@ -8,44 +8,60 @@ import (
)
type (
BasicValidateFunc func(string, string) bool
// BasicAuth defines an HTTP basic authentication middleware.
BasicAuth struct {
function BasicAuthFunc
priority int
}
BasicAuthFunc func(string, string) bool
)
const (
basic = "Basic"
)
// BasicAuth returns an HTTP basic authentication middleware.
func NewBasicAuth(fn BasicAuthFunc) *BasicAuth {
return &BasicAuth{function: fn}
}
func (ba *BasicAuth) SetPriority(p int) {
ba.priority = p
}
func (ba *BasicAuth) Priority() int {
return ba.priority
}
// Handle validates credentials using `AuthFunc`
//
// For valid credentials it calls the next handler.
// For invalid credentials, it sends "401 - Unauthorized" response.
func BasicAuth(fn BasicValidateFunc) echo.MiddlewareFunc {
return func(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
// Skip WebSocket
if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket {
return nil
}
func (ba *BasicAuth) Handle(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
// Skip WebSocket
if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket {
return nil
}
auth := c.Request().Header().Get(echo.Authorization)
l := len(basic)
auth := c.Request().Header().Get(echo.Authorization)
l := len(basic)
if len(auth) > l+1 && auth[:l] == basic {
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
if err == nil {
cred := string(b)
for i := 0; i < len(cred); i++ {
if cred[i] == ':' {
// Verify credentials
if fn(cred[:i], cred[i+1:]) {
return nil
}
if len(auth) > l+1 && auth[:l] == basic {
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
if err == nil {
cred := string(b)
for i := 0; i < len(cred); i++ {
if cred[i] == ':' {
// Verify credentials
if ba.function(cred[:i], cred[i+1:]) {
return nil
}
}
}
}
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
return echo.NewHTTPError(http.StatusUnauthorized)
})
}
}
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
return echo.NewHTTPError(http.StatusUnauthorized)
})
}

View File

@ -21,7 +21,7 @@ func TestBasicAuth(t *testing.T) {
}
return false
}
h := BasicAuth(fn)(echo.HandlerFunc(func(c echo.Context) error {
h := NewBasicAuth(fn).Handle(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
}))

View File

@ -15,12 +15,57 @@ import (
)
type (
Gzip struct {
level int
priority int
}
gzipWriter struct {
io.Writer
engine.Response
}
)
func NewGzip() *Gzip {
return &Gzip{}
}
func (g *Gzip) SetLevel(l int) {
g.level = l
}
func (g *Gzip) SetPriority(p int) {
g.priority = p
}
func (g *Gzip) Priority() int {
return g.priority
}
// Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme.
func (*Gzip) Handle(h 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) {
w := writerPool.Get().(*gzip.Writer)
w.Reset(c.Response().Writer())
defer func() {
w.Close()
writerPool.Put(w)
}()
gw := gzipWriter{Writer: w, Response: c.Response()}
c.Response().Header().Set(echo.ContentEncoding, scheme)
c.Response().SetWriter(gw)
}
if err := h.Handle(c); err != nil {
c.Error(err)
}
return nil
})
}
func (w gzipWriter) Write(b []byte) (int, error) {
if w.Header().Get(echo.ContentType) == "" {
w.Header().Set(echo.ContentType, http.DetectContentType(b))
@ -45,29 +90,3 @@ var writerPool = sync.Pool{
return gzip.NewWriter(ioutil.Discard)
},
}
// Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme.
func Gzip() echo.MiddlewareFunc {
return func(h 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) {
w := writerPool.Get().(*gzip.Writer)
w.Reset(c.Response().Writer())
defer func() {
w.Close()
writerPool.Put(w)
}()
gw := gzipWriter{Writer: w, Response: c.Response()}
c.Response().Header().Set(echo.ContentEncoding, scheme)
c.Response().SetWriter(gw)
}
if err := h.Handle(c); err != nil {
c.Error(err)
}
return nil
})
}
}

View File

@ -36,12 +36,11 @@ func TestGzip(t *testing.T) {
rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e)
// Skip if no Accept-Encoding header
h := Gzip()(echo.HandlerFunc(func(c echo.Context) error {
h := NewGzip().Handle(echo.HandlerFunc(func(c echo.Context) error {
c.Response().Write([]byte("test")) // For Content-Type sniffing
return nil
}))
h.Handle(c)
// assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, "test", rec.Body.String())
req = test.NewRequest(echo.GET, "/", nil)

View File

@ -8,47 +8,63 @@ import (
"github.com/labstack/gommon/color"
)
func Log() echo.MiddlewareFunc {
return func(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
req := c.Request()
res := c.Response()
logger := c.Logger()
remoteAddr := req.RemoteAddress()
if ip := req.Header().Get(echo.XRealIP); ip != "" {
remoteAddr = ip
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
start := time.Now()
if err := h.Handle(c); err != nil {
c.Error(err)
}
stop := time.Now()
method := req.Method()
path := req.URL().Path()
if path == "" {
path = "/"
}
size := res.Size()
n := res.Status()
code := color.Green(n)
switch {
case n >= 500:
code = color.Red(n)
case n >= 400:
code = color.Yellow(n)
case n >= 300:
code = color.Cyan(n)
}
logger.Infof("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
return nil
})
type (
Log struct {
priority int
}
)
func NewLog() *Log {
return &Log{}
}
func (l *Log) SetPriority(p int) {
l.priority = p
}
func (l *Log) Priority() int {
return l.priority
}
func (*Log) Handle(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
req := c.Request()
res := c.Response()
logger := c.Logger()
remoteAddr := req.RemoteAddress()
if ip := req.Header().Get(echo.XRealIP); ip != "" {
remoteAddr = ip
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
start := time.Now()
if err := h.Handle(c); err != nil {
c.Error(err)
}
stop := time.Now()
method := req.Method()
path := req.URL().Path()
if path == "" {
path = "/"
}
size := res.Size()
n := res.Status()
code := color.Green(n)
switch {
case n >= 500:
code = color.Red(n)
case n >= 400:
code = color.Yellow(n)
case n >= 300:
code = color.Cyan(n)
}
logger.Infof("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
return nil
})
}

View File

@ -18,7 +18,8 @@ func TestLog(t *testing.T) {
req := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e)
h := Log()(echo.HandlerFunc(func(c echo.Context) error {
l := NewLog()
h := l.Handle(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
}))
@ -28,7 +29,7 @@ func TestLog(t *testing.T) {
// Status 3xx
rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e)
h = Log()(echo.HandlerFunc(func(c echo.Context) error {
h = l.Handle(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusTemporaryRedirect, "test")
}))
h.Handle(c)
@ -36,7 +37,7 @@ func TestLog(t *testing.T) {
// Status 4xx
rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e)
h = Log()(echo.HandlerFunc(func(c echo.Context) error {
h = l.Handle(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusNotFound, "test")
}))
h.Handle(c)
@ -45,7 +46,7 @@ func TestLog(t *testing.T) {
req = test.NewRequest(echo.GET, "", nil)
rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e)
h = Log()(echo.HandlerFunc(func(c echo.Context) error {
h = l.Handle(echo.HandlerFunc(func(c echo.Context) error {
return errors.New("error")
}))
h.Handle(c)
@ -59,7 +60,7 @@ func TestLogIPAddress(t *testing.T) {
buf := new(bytes.Buffer)
e.Logger().(*log.Logger).SetOutput(buf)
ip := "127.0.0.1"
h := Log()(echo.HandlerFunc(func(c echo.Context) error {
h := NewLog().Handle(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
}))

View File

@ -8,21 +8,37 @@ import (
"github.com/labstack/echo"
)
type (
Recover struct {
priority int
}
)
func NewRecover() *Recover {
return &Recover{}
}
func (r *Recover) SetPriority(p int) {
r.priority = p
}
func (r *Recover) Priority() int {
return r.priority
}
// Recover returns a middleware which recovers from panics anywhere in the chain
// and handles the control to the centralized HTTPErrorHandler.
func Recover() echo.MiddlewareFunc {
return func(h 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 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]))
}
}()
return h.Handle(c)
})
}
func (*Recover) Handle(h 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 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]))
}
}()
return h.Handle(c)
})
}

View File

@ -15,7 +15,7 @@ func TestRecover(t *testing.T) {
req := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e)
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
h := NewRecover().Handle(echo.HandlerFunc(func(c echo.Context) error {
panic("test")
}))
h.Handle(c)