1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-10 00:28:23 +02:00
echo/website/content/guide/faq.md
Vishal Rana f4b0004d2b website/recipe in the main repo
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-10-20 09:11:07 -07:00

1.8 KiB

+++ title = "FAQ" description = "Frequently asked questions in Echo" [menu.side] name = "FAQ" parent = "guide" weight = 20 +++

FAQ

Q: How to retrieve *http.Request and http.ResponseWriter from echo.Context?

  • http.Request > c.Request().(*standard.Request).Request
  • http.ResponseWriter > c.Response()

Standard engine only

Q: How to use standard handler func(http.ResponseWriter, *http.Request) with Echo?

func handler(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Handler!")
}

func main() {
	e := echo.New()
	e.GET("/", standard.WrapHandler(http.HandlerFunc(handler)))
	e.Run(standard.New(":1323"))
}

Q: How to use fasthttp handler func(fasthttp.RequestCtx) with Echo?

func handler(c *fh.RequestCtx) {
	io.WriteString(c, "Handler!")
}

func main() {
	e := echo.New()
	e.GET("/", fasthttp.WrapHandler(handler))
	e.Run(fasthttp.New(":1323"))
}

Q: How to use standard middleware func(http.Handler) http.Handler with Echo?

func middleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		println("Middleware!")
		h.ServeHTTP(w, r)
	})
}

func main() {
	e := echo.New()
	e.Use(standard.WrapMiddleware(middleware))
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "OK")
	})
	e.Run(standard.New(":1323"))
}

Q: How to use fasthttp middleware func(http.Handler) http.Handler with Echo?

func middleware(h fh.RequestHandler) fh.RequestHandler {
	return func(ctx *fh.RequestCtx) {
		println("Middleware!")
		h(ctx)
	}
}

func main() {
	e := echo.New()
	e.Use(fasthttp.WrapMiddleware(middleware))
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "OK")
	})
	e.Run(fasthttp.New(":1323"))
}