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:
parent
c9a62e20b4
commit
88f307bedd
@ -10,22 +10,22 @@ 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 Static(root string, option ...*StaticOption) echo.HandlerFunc {
|
||||
// Default options
|
||||
opt := &StaticOption{Index: "index.html"}
|
||||
if len(option) > 0 {
|
||||
opt = option[0]
|
||||
}
|
||||
}
|
||||
|
||||
func (s Static) Handle(c echo.Context) error {
|
||||
fs := http.Dir(s.Root)
|
||||
return func(c echo.Context) error {
|
||||
fs := http.Dir(root)
|
||||
file := c.P(0)
|
||||
f, err := fs.Open(file)
|
||||
if err != nil {
|
||||
@ -46,10 +46,10 @@ func (s Static) Handle(c echo.Context) error {
|
||||
d := f
|
||||
|
||||
// Index file
|
||||
file = path.Join(file, s.Index)
|
||||
file = path.Join(file, opt.Index)
|
||||
f, err = fs.Open(file)
|
||||
if err != nil {
|
||||
if s.Browse {
|
||||
if opt.Browse {
|
||||
dirs, err := d.Readdir(-1)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -84,6 +84,7 @@ func (s Static) Handle(c echo.Context) error {
|
||||
return nil
|
||||
// TODO:
|
||||
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
|
||||
}
|
||||
}
|
||||
|
||||
// Favicon serves the default favicon - GET /favicon.ico.
|
||||
|
@ -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,23 +18,12 @@ 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 {
|
||||
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 {
|
||||
@ -54,7 +40,7 @@ func (ba *BasicAuth) Handle(h echo.Handler) echo.Handler {
|
||||
for i := 0; i < len(cred); i++ {
|
||||
if cred[i] == ':' {
|
||||
// Verify credentials
|
||||
if ba.function(cred[:i], cred[i+1:]) {
|
||||
if fn(cred[:i], cred[i+1:]) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -64,4 +50,5 @@ func (ba *BasicAuth) Handle(h echo.Handler) echo.Handler {
|
||||
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
|
||||
return echo.NewHTTPError(http.StatusUnauthorized)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
}))
|
||||
|
||||
|
@ -15,9 +15,8 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
Gzip struct {
|
||||
GzipOption struct {
|
||||
level int
|
||||
priority int
|
||||
}
|
||||
|
||||
gzipWriter struct {
|
||||
@ -26,25 +25,10 @@ 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 {
|
||||
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)
|
||||
@ -64,6 +48,7 @@ func (*Gzip) Handle(h echo.Handler) echo.Handler {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (w gzipWriter) Write(b []byte) (int, error) {
|
||||
|
@ -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
|
||||
}))
|
||||
|
@ -9,24 +9,12 @@ 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 {
|
||||
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()
|
||||
@ -67,4 +55,5 @@ func (*Log) Handle(h echo.Handler) echo.Handler {
|
||||
logger.Infof("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
}))
|
||||
|
||||
|
@ -9,26 +9,14 @@ 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 {
|
||||
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() {
|
||||
@ -41,4 +29,5 @@ func (*Recover) Handle(h echo.Handler) echo.Handler {
|
||||
}()
|
||||
return h.Handle(c)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user