2016-10-20 20:30:53 +02:00
|
|
|
+++
|
|
|
|
title = "FAQ"
|
|
|
|
description = "Frequently asked questions in Echo"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
2016-10-20 20:30:53 +02:00
|
|
|
name = "FAQ"
|
|
|
|
parent = "guide"
|
|
|
|
weight = 20
|
|
|
|
+++
|
|
|
|
|
2016-11-13 19:36:57 +02:00
|
|
|
Q: How to retrieve `*http.Request` and `http.ResponseWriter` from `echo.Context`?
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-11-13 19:36:57 +02:00
|
|
|
- `http.Request` > `c.Request()`
|
2016-10-20 20:30:53 +02:00
|
|
|
- `http.ResponseWriter` > `c.Response()`
|
|
|
|
|
2016-11-13 19:36:57 +02:00
|
|
|
Q: How to use standard handler `func(http.ResponseWriter, *http.Request)` with Echo?
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
2016-11-13 19:36:57 +02:00
|
|
|
io.WriteString(w, "Echo!")
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
2016-11-13 19:36:57 +02:00
|
|
|
e.GET("/", echo.WrapHandler(http.HandlerFunc(handler)))
|
|
|
|
e.Start(":1323")
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-13 19:36:57 +02:00
|
|
|
Q: How to use standard middleware `func(http.Handler) http.Handler` with Echo?
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
func middleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-11-13 19:36:57 +02:00
|
|
|
println("middleware")
|
2016-10-20 20:30:53 +02:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
2016-11-13 19:36:57 +02:00
|
|
|
e.Use(echo.WrapMiddleware(middleware))
|
2016-10-20 20:30:53 +02:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2016-11-13 19:36:57 +02:00
|
|
|
return c.String(http.StatusOK, "Echo!")
|
2016-10-20 20:30:53 +02:00
|
|
|
})
|
2016-11-13 19:36:57 +02:00
|
|
|
e.Start(":1323")
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-13 19:36:57 +02:00
|
|
|
Q: How to run Echo on a specific IP address?
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
2016-11-13 19:36:57 +02:00
|
|
|
e.Start("<ip>:<port>")
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|