From af4c525be13f250ddadfa3e0bfae333d14eb8ef5 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 6 Mar 2016 11:38:52 -0800 Subject: [PATCH] Wrapper for handler in engine Signed-off-by: Vishal Rana --- engine/fasthttp/server.go | 10 ++++++++++ engine/standard/server.go | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/engine/fasthttp/server.go b/engine/fasthttp/server.go index d2f27141..5317a23b 100644 --- a/engine/fasthttp/server.go +++ b/engine/fasthttp/server.go @@ -5,6 +5,7 @@ package fasthttp import ( "sync" + "github.com/labstack/echo" "github.com/labstack/echo/engine" "github.com/labstack/gommon/log" "github.com/valyala/fasthttp" @@ -121,3 +122,12 @@ func (s *Server) Start() { s.logger.Fatal(fasthttp.ListenAndServe(addr, handler)) } } + +// WrapHandler wraps `fasthttp.RequestHandler` into `echo.Handler`. +func WrapHandler(h fasthttp.RequestHandler) echo.Handler { + return echo.HandlerFunc(func(c echo.Context) error { + c := c.Request().Object().(*fasthttp.RequestCtx) + h(c) + return nil + }) +} diff --git a/engine/standard/server.go b/engine/standard/server.go index 55729ca9..0e5033f4 100644 --- a/engine/standard/server.go +++ b/engine/standard/server.go @@ -4,6 +4,7 @@ import ( "net/http" "sync" + "github.com/labstack/echo" "github.com/labstack/echo/engine" "github.com/labstack/gommon/log" ) @@ -116,3 +117,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.pool.response.Put(res) 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 { + w := c.Response().Object().(http.ResponseWriter) + r := c.Request().Object().(*http.Request) + h.ServeHTTP(w, r) + return nil + }) +}