1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Middleware and handler as closure

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-02-17 21:49:31 -08:00
parent c9a62e20b4
commit 88f307bedd
9 changed files with 180 additions and 230 deletions

View File

@ -10,80 +10,81 @@ import (
) )
type ( type (
Static struct { StaticOption 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 NewStatic(root string) *Static { func Static(root string, option ...*StaticOption) echo.HandlerFunc {
return &Static{ // Default options
Root: root, opt := &StaticOption{Index: "index.html"}
Index: "index.html", if len(option) > 0 {
} opt = option[0]
}
func (s Static) Handle(c echo.Context) error {
fs := http.Dir(s.Root)
file := c.P(0)
f, err := fs.Open(file)
if err != nil {
return echo.ErrNotFound
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
} }
if fi.IsDir() { return func(c echo.Context) error {
/* NOTE: fs := http.Dir(root)
Not checking the Last-Modified header as it caches the response `304` when file := c.P(0)
changing differnt directories for the same path. f, err := fs.Open(file)
*/
d := f
// Index file
file = path.Join(file, s.Index)
f, err = fs.Open(file)
if err != nil { if err != nil {
if s.Browse {
dirs, err := d.Readdir(-1)
if err != nil {
return err
}
// Create a directory index
res := c.Response()
res.Header().Set(echo.ContentType, echo.TextHTMLCharsetUTF8)
if _, err = fmt.Fprintf(res, "<pre>\n"); err != nil {
return err
}
for _, d := range dirs {
name := d.Name()
color := "#212121"
if d.IsDir() {
color = "#e91e63"
name += "/"
}
if _, err = fmt.Fprintf(res, "<a href=\"%s\" style=\"color: %s;\">%s</a>\n", name, color, name); err != nil {
return err
}
}
_, err = fmt.Fprintf(res, "</pre>\n")
return err
}
return echo.ErrNotFound return echo.ErrNotFound
} }
fi, _ = f.Stat() // Index file stat defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
/* NOTE:
Not checking the Last-Modified header as it caches the response `304` when
changing differnt directories for the same path.
*/
d := f
// Index file
file = path.Join(file, opt.Index)
f, err = fs.Open(file)
if err != nil {
if opt.Browse {
dirs, err := d.Readdir(-1)
if err != nil {
return err
}
// Create a directory index
res := c.Response()
res.Header().Set(echo.ContentType, echo.TextHTMLCharsetUTF8)
if _, err = fmt.Fprintf(res, "<pre>\n"); err != nil {
return err
}
for _, d := range dirs {
name := d.Name()
color := "#212121"
if d.IsDir() {
color = "#e91e63"
name += "/"
}
if _, err = fmt.Fprintf(res, "<a href=\"%s\" style=\"color: %s;\">%s</a>\n", name, color, name); err != nil {
return err
}
}
_, err = fmt.Fprintf(res, "</pre>\n")
return err
}
return echo.ErrNotFound
}
fi, _ = f.Stat() // Index file stat
}
c.Response().WriteHeader(http.StatusOK)
io.Copy(c.Response(), f)
return nil
// TODO:
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
} }
c.Response().WriteHeader(http.StatusOK)
io.Copy(c.Response(), f)
return nil
// TODO:
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
} }
// Favicon serves the default favicon - GET /favicon.ico. // Favicon serves the default favicon - GET /favicon.ico.

View File

@ -8,10 +8,7 @@ import (
) )
type ( type (
// BasicAuth defines an HTTP basic authentication middleware. BasicAuthOption struct {
BasicAuth struct {
function BasicAuthFunc
priority int
} }
BasicAuthFunc func(string, string) bool BasicAuthFunc func(string, string) bool
@ -21,47 +18,37 @@ const (
basic = "Basic" basic = "Basic"
) )
func NewBasicAuth(fn BasicAuthFunc) *BasicAuth { // BasicAuth returns an HTTP basic authentication middleware.
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 valid credentials it calls the next handler.
// For invalid credentials, it sends "401 - Unauthorized" response. // For invalid credentials, it sends "401 - Unauthorized" response.
func (ba *BasicAuth) Handle(h echo.Handler) echo.Handler { func BasicAuth(fn BasicAuthFunc, option ...*BasicAuthOption) echo.MiddlewareFunc {
return echo.HandlerFunc(func(c echo.Context) error { return func(h echo.Handler) echo.Handler {
// Skip WebSocket return echo.HandlerFunc(func(c echo.Context) error {
if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket { // Skip WebSocket
return nil if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket {
} return nil
}
auth := c.Request().Header().Get(echo.Authorization) auth := c.Request().Header().Get(echo.Authorization)
l := len(basic) l := len(basic)
if len(auth) > l+1 && auth[:l] == basic { if len(auth) > l+1 && auth[:l] == basic {
b, err := base64.StdEncoding.DecodeString(auth[l+1:]) b, err := base64.StdEncoding.DecodeString(auth[l+1:])
if err == nil { if err == nil {
cred := string(b) cred := string(b)
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 ba.function(cred[:i], cred[i+1:]) { if fn(cred[:i], cred[i+1:]) {
return nil return nil
}
} }
} }
} }
} }
} c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted") return echo.NewHTTPError(http.StatusUnauthorized)
return echo.NewHTTPError(http.StatusUnauthorized) })
}) }
} }

View File

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

View File

@ -15,9 +15,8 @@ import (
) )
type ( type (
Gzip struct { GzipOption struct {
level int level int
priority int
} }
gzipWriter struct { gzipWriter struct {
@ -26,44 +25,30 @@ type (
} }
) )
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 // Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme. // scheme.
func (*Gzip) Handle(h echo.Handler) echo.Handler { func Gzip(option ...*GzipOption) echo.MiddlewareFunc {
scheme := "gzip" return func(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error { scheme := "gzip"
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding) return echo.HandlerFunc(func(c echo.Context) error {
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) { c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
w := writerPool.Get().(*gzip.Writer) if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
w.Reset(c.Response().Writer()) w := writerPool.Get().(*gzip.Writer)
defer func() { w.Reset(c.Response().Writer())
w.Close() defer func() {
writerPool.Put(w) w.Close()
}() writerPool.Put(w)
gw := gzipWriter{Writer: w, Response: c.Response()} }()
c.Response().Header().Set(echo.ContentEncoding, scheme) gw := gzipWriter{Writer: w, Response: c.Response()}
c.Response().SetWriter(gw) c.Response().Header().Set(echo.ContentEncoding, scheme)
} c.Response().SetWriter(gw)
if err := h.Handle(c); err != nil { }
c.Error(err) if err := h.Handle(c); err != nil {
} c.Error(err)
return nil }
}) return nil
})
}
} }
func (w gzipWriter) Write(b []byte) (int, error) { func (w gzipWriter) Write(b []byte) (int, error) {

View File

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

View File

@ -9,62 +9,51 @@ import (
) )
type ( type (
Log struct { LogOption struct {
priority int
} }
) )
func NewLog() *Log { func Log(option ...*LogOption) echo.MiddlewareFunc {
return &Log{} return func(h echo.Handler) echo.Handler {
} return echo.HandlerFunc(func(c echo.Context) error {
req := c.Request()
func (l *Log) SetPriority(p int) { res := c.Response()
l.priority = p logger := c.Logger()
}
remoteAddr := req.RemoteAddress()
func (l *Log) Priority() int { if ip := req.Header().Get(echo.XRealIP); ip != "" {
return l.priority remoteAddr = ip
} } else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
remoteAddr = ip
func (*Log) Handle(h echo.Handler) echo.Handler { } else {
return echo.HandlerFunc(func(c echo.Context) error { remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
req := c.Request() }
res := c.Response()
logger := c.Logger() start := time.Now()
if err := h.Handle(c); err != nil {
remoteAddr := req.RemoteAddress() c.Error(err)
if ip := req.Header().Get(echo.XRealIP); ip != "" { }
remoteAddr = ip stop := time.Now()
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" { method := req.Method()
remoteAddr = ip path := req.URL().Path()
} else { if path == "" {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr) path = "/"
} }
size := res.Size()
start := time.Now()
if err := h.Handle(c); err != nil { n := res.Status()
c.Error(err) code := color.Green(n)
} switch {
stop := time.Now() case n >= 500:
method := req.Method() code = color.Red(n)
path := req.URL().Path() case n >= 400:
if path == "" { code = color.Yellow(n)
path = "/" case n >= 300:
} code = color.Cyan(n)
size := res.Size() }
n := res.Status() logger.Infof("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
code := color.Green(n) return nil
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,8 +18,7 @@ func TestLog(t *testing.T) {
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)
l := NewLog() h := Log()(echo.HandlerFunc(func(c echo.Context) error {
h := l.Handle(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")
})) }))
@ -29,7 +28,7 @@ func TestLog(t *testing.T) {
// Status 3xx // Status 3xx
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e) c = echo.NewContext(req, rec, e)
h = l.Handle(echo.HandlerFunc(func(c echo.Context) error { h = Log()(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusTemporaryRedirect, "test") return c.String(http.StatusTemporaryRedirect, "test")
})) }))
h.Handle(c) h.Handle(c)
@ -37,7 +36,7 @@ func TestLog(t *testing.T) {
// Status 4xx // Status 4xx
rec = test.NewResponseRecorder() rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e) c = echo.NewContext(req, rec, e)
h = l.Handle(echo.HandlerFunc(func(c echo.Context) error { h = Log()(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusNotFound, "test") return c.String(http.StatusNotFound, "test")
})) }))
h.Handle(c) h.Handle(c)
@ -46,7 +45,7 @@ func TestLog(t *testing.T) {
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)
h = l.Handle(echo.HandlerFunc(func(c echo.Context) error { h = Log()(echo.HandlerFunc(func(c echo.Context) error {
return errors.New("error") return errors.New("error")
})) }))
h.Handle(c) h.Handle(c)
@ -60,7 +59,7 @@ func TestLogIPAddress(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
e.Logger().(*log.Logger).SetOutput(buf) e.Logger().(*log.Logger).SetOutput(buf)
ip := "127.0.0.1" ip := "127.0.0.1"
h := NewLog().Handle(echo.HandlerFunc(func(c echo.Context) error { h := Log()(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")
})) }))

View File

@ -9,36 +9,25 @@ import (
) )
type ( type (
Recover struct { RecoverOption 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 // 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) Handle(h echo.Handler) echo.Handler { func Recover(option ...*RecoverOption) echo.MiddlewareFunc {
// TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace` return func(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error { // TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace`
defer func() { return echo.HandlerFunc(func(c echo.Context) error {
if err := recover(); err != nil { defer func() {
trace := make([]byte, 1<<16) if err := recover(); err != nil {
n := runtime.Stack(trace, true) trace := make([]byte, 1<<16)
c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s", n := runtime.Stack(trace, true)
err, n, trace[:n])) c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s",
} err, n, trace[:n]))
}() }
return h.Handle(c) }()
}) return h.Handle(c)
})
}
} }

View File

@ -15,7 +15,7 @@ func TestRecover(t *testing.T) {
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)
h := NewRecover().Handle(echo.HandlerFunc(func(c echo.Context) error { h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
panic("test") panic("test")
})) }))
h.Handle(c) h.Handle(c)