1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00
echo/website/content/guide/faq.md
Vishal Rana 054a310e70 updated website
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-11-20 14:51:17 -08:00

1.1 KiB

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

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

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

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

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

func main() {
	e := echo.New()
	e.GET("/", echo.WrapHandler(http.HandlerFunc(handler)))
	e.Start(":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(echo.WrapMiddleware(middleware))
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Echo!")
	})
	e.Start(":1323")
}

Q: How to run Echo on a specific IP address?

e.Start("<ip>:<port>")