mirror of
https://github.com/labstack/echo.git
synced 2025-07-05 00:58:47 +02:00
49
echo.go
49
echo.go
@ -3,39 +3,36 @@ Package echo implements a fast and unfancy HTTP server framework for Go (Golang)
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/labstack/echo/engine/standard"
|
"github.com/labstack/echo/middleware"
|
||||||
"github.com/labstack/echo/middleware"
|
)
|
||||||
"net"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Handler
|
// Handler
|
||||||
func hello(c echo.Context) error {
|
func hello(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "Hello, World!")
|
return c.String(http.StatusOK, "Hello, World!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Echo instance
|
// Echo instance
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
e.Use(middleware.Logger())
|
e.Use(middleware.Logger())
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
e.GET("/", hello)
|
e.GET("/", hello)
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
if err := e.Start(":1323"); err != nil {
|
if err := e.Start(":1323"); err != nil {
|
||||||
e.Logger.Fatal(err)
|
e.Logger.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Learn more at https://echo.labstack.com
|
Learn more at https://echo.labstack.com
|
||||||
*/
|
*/
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
+++
|
|
||||||
title = "engine"
|
|
||||||
[menu.side]
|
|
||||||
name = "engine"
|
|
||||||
parent = "godoc"
|
|
||||||
weight = 3
|
|
||||||
url = "https://godoc.org/github.com/labstack/echo/engine"
|
|
||||||
+++
|
|
@ -1,8 +0,0 @@
|
|||||||
+++
|
|
||||||
title = "fasthttp"
|
|
||||||
[menu.side]
|
|
||||||
name = "engine/fasthttp"
|
|
||||||
parent = "godoc"
|
|
||||||
weight = 5
|
|
||||||
url = "https://godoc.org/github.com/labstack/echo/engine/fasthttp"
|
|
||||||
+++
|
|
@ -4,6 +4,6 @@ title = "middleware"
|
|||||||
name = "middleware"
|
name = "middleware"
|
||||||
identifier = "godoc-middleware"
|
identifier = "godoc-middleware"
|
||||||
parent = "godoc"
|
parent = "godoc"
|
||||||
weight = 1
|
weight = 2
|
||||||
url = "https://godoc.org/github.com/labstack/echo/middleware"
|
url = "https://godoc.org/github.com/labstack/echo/middleware"
|
||||||
+++
|
+++
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
+++
|
|
||||||
title = "standard"
|
|
||||||
[menu.side]
|
|
||||||
name = "engine/standard"
|
|
||||||
parent = "godoc"
|
|
||||||
weight = 4
|
|
||||||
url = "https://godoc.org/github.com/labstack/echo/engine/standard"
|
|
||||||
+++
|
|
@ -12,8 +12,8 @@ description = "Context in Echo"
|
|||||||
|
|
||||||
`echo.Context` represents the context of the current HTTP request. It holds request and
|
`echo.Context` represents the context of the current HTTP request. It holds request and
|
||||||
response reference, path, path parameters, data, registered handler and APIs to read
|
response reference, path, path parameters, data, registered handler and APIs to read
|
||||||
request and write response. Context is 100% compatible with standard `context.Context`.
|
request and write response. As Context is an interface, it is easy to extend it with
|
||||||
As Context is an interface, it is easy to extend it with custom APIs.
|
custom APIs.
|
||||||
|
|
||||||
#### Extending Context
|
#### Extending Context
|
||||||
|
|
||||||
@ -56,20 +56,3 @@ e.GET("/", func(c echo.Context) error {
|
|||||||
return cc.String(200, "OK")
|
return cc.String(200, "OK")
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Standard Context
|
|
||||||
|
|
||||||
`echo.Context` embeds standard `context.Context` interface, so all it's functions
|
|
||||||
are available right from `echo.Context`.
|
|
||||||
|
|
||||||
*Example*
|
|
||||||
|
|
||||||
```go
|
|
||||||
e.GET("/users/:name", func(c echo.Context) error) {
|
|
||||||
c.SetContext(context.WithValue(nil, "key", "val"))
|
|
||||||
// Pass it down...
|
|
||||||
// Use it...
|
|
||||||
val := c.Value("key").(string)
|
|
||||||
return c.String(http.StatusOK, name)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
Reference in New Issue
Block a user