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

Middleware wrapper #294

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-06 22:05:53 -08:00
parent 6a74849290
commit b855b5d507
2 changed files with 35 additions and 8 deletions

View File

@ -123,11 +123,24 @@ func (s *Server) Start() {
}
}
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.Handler`.
func WrapHandler(h fasthttp.RequestHandler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Object().(*fasthttp.RequestCtx)
h(ctx)
return nil
})
}
}
// WrapMiddleware wraps `fasthttp.RequestHandler` into `echo.MiddlewareFunc`
func WrapMiddleware(m fasthttp.RequestHandler) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
ctx := c.Request().Object().(*fasthttp.RequestCtx)
if !c.Response().Committed() {
m(ctx)
}
return next.Handle(c)
})
}
}

View File

@ -118,12 +118,26 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.pool.header.Put(resHdr)
}
// WrapHandler wraps `http.Handler` into `echo.Handler`.
func WrapHandler(h http.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
func WrapHandler(h http.Handler) echo.HandlerFunc {
return func(c echo.Context) error {
w := c.Response().Object().(http.ResponseWriter)
r := c.Request().Object().(*http.Request)
h.ServeHTTP(w, r)
return nil
})
}
}
// WrapMiddleware wraps `http.Handler` into `echo.MiddlewareFunc`
func WrapMiddleware(m http.Handler) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
w := c.Response().Object().(http.ResponseWriter)
r := c.Request().Object().(*http.Request)
if !c.Response().Committed() {
m.ServeHTTP(w, r)
}
return next.Handle(c)
})
}
}