1
0
mirror of https://github.com/labstack/echo.git synced 2026-05-16 09:48:24 +02:00
Files
echo/website/content/guide/faq.md
T
Vishal Rana 52fa135fd5 Updated docs, exposed middleware.DefaultSkipper
Signed-off-by: Vishal Rana <vr@labstack.com>
2017-01-28 11:46:43 -08:00

1.5 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>")

Q: How to run Echo on a random port?

e.Start(":0")

Q: How to run Echo on a unix domain socket?

e := echo.New()
e.GET("/", func(c echo.Context) error {
  return c.String(http.StatusOK, "OK")
})
os.Remove("/tmp/echo.sock")
l, err := net.Listen("unix", "/tmp/echo.sock")
if err != nil {
  e.Logger.Fatal(err)
}
e.Listener = l
e.Logger.Fatal(e.Start(""))
curl --unix-socket /tmp/echo.sock http://localhost