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 (
Static struct {
StaticOption struct {
Root string `json:"root"`
Index string `json:"index"`
Browse bool `json:"browse"`
}
)
func NewStatic(root string) *Static {
return &Static{
Root: root,
Index: "index.html",
}
}
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
func Static(root string, option ...*StaticOption) echo.HandlerFunc {
// Default options
opt := &StaticOption{Index: "index.html"}
if len(option) > 0 {
opt = option[0]
}
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, s.Index)
f, err = fs.Open(file)
return func(c echo.Context) error {
fs := http.Dir(root)
file := c.P(0)
f, err := fs.Open(file)
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
}
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.

View File

@ -8,10 +8,7 @@ import (
)
type (
// BasicAuth defines an HTTP basic authentication middleware.
BasicAuth struct {
function BasicAuthFunc
priority int
BasicAuthOption struct {
}
BasicAuthFunc func(string, string) bool
@ -21,47 +18,37 @@ const (
basic = "Basic"
)
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`
// BasicAuth returns an HTTP basic authentication middleware.
//
// For valid credentials it calls the next handler.
// For invalid credentials, it sends "401 - Unauthorized" response.
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
}
func BasicAuth(fn BasicAuthFunc, option ...*BasicAuthOption) 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
}
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 ba.function(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 fn(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 := 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")
}))

View File

@ -15,9 +15,8 @@ import (
)
type (
Gzip struct {
level int
priority int
GzipOption struct {
level int
}
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
// 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 Gzip(option ...*GzipOption) 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
})
}
}
func (w gzipWriter) Write(b []byte) (int, error) {

View File

@ -36,7 +36,7 @@ func TestGzip(t *testing.T) {
rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e)
// 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
return nil
}))

View File

@ -9,62 +9,51 @@ import (
)
type (
Log struct {
priority int
LogOption struct {
}
)
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
})
func Log(option ...*LogOption) 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
})
}
}

View File

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

View File

@ -9,36 +9,25 @@ import (
)
type (
Recover struct {
priority int
RecoverOption struct {
}
)
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) 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)
})
func Recover(option ...*RecoverOption) 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)
})
}
}

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 := NewRecover().Handle(echo.HandlerFunc(func(c echo.Context) error {
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
panic("test")
}))
h.Handle(c)