2016-10-20 20:30:53 +02:00
|
|
|
+++
|
2016-11-20 08:13:05 +02:00
|
|
|
title = "Guide"
|
|
|
|
description = "Guide"
|
|
|
|
type = "guide"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
|
|
|
name = "Guide"
|
|
|
|
pre = "<i class='fa fa-book'></i>"
|
|
|
|
weight = 1
|
|
|
|
identifier = "guide"
|
|
|
|
url = "/guide"
|
2016-10-20 20:30:53 +02:00
|
|
|
+++
|
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
|
|
|
|
### Installation
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ go get -u github.com/labstack/echo
|
|
|
|
```
|
|
|
|
|
|
|
|
### Hello, World!
|
|
|
|
|
|
|
|
Create `server.go`
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-11-08 20:19:30 +02:00
|
|
|
|
2016-10-20 20:30:53 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Hello, World!")
|
|
|
|
})
|
2016-11-18 02:21:41 +02:00
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Start server
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ go run server.go
|
|
|
|
```
|
|
|
|
|
|
|
|
Browse to [http://localhost:1323](http://localhost:1323) and you should see
|
|
|
|
Hello, World! on the page.
|
|
|
|
|
|
|
|
### Routing
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.POST("/users", saveUser)
|
|
|
|
e.GET("/users/:id", getUser)
|
|
|
|
e.PUT("/users/:id", updateUser)
|
|
|
|
e.DELETE("/users/:id", deleteUser)
|
|
|
|
```
|
|
|
|
|
|
|
|
### Path Parameters
|
|
|
|
|
|
|
|
```go
|
2016-11-08 20:19:30 +02:00
|
|
|
// e.GET("/users/:id", getUser)
|
2016-10-20 20:30:53 +02:00
|
|
|
func getUser(c echo.Context) error {
|
2016-11-08 20:19:30 +02:00
|
|
|
// User ID from path `users/:id`
|
|
|
|
id := c.Param("id")
|
2016-11-01 17:33:56 +02:00
|
|
|
return c.String(http.StatusOK, id)
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
```
|
2016-11-08 20:19:30 +02:00
|
|
|
|
2016-11-01 17:33:56 +02:00
|
|
|
Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
### Query Parameters
|
|
|
|
|
|
|
|
`/show?team=x-men&member=wolverine`
|
|
|
|
|
|
|
|
```go
|
2016-11-08 20:19:30 +02:00
|
|
|
//e.GET("/show", show)
|
2016-10-20 20:30:53 +02:00
|
|
|
func show(c echo.Context) error {
|
|
|
|
// Get team and member from the query string
|
|
|
|
team := c.QueryParam("team")
|
|
|
|
member := c.QueryParam("member")
|
2016-11-01 17:33:56 +02:00
|
|
|
return c.String(http.StatusOK, "team:" + team + ", member:" + member)
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-01 17:33:56 +02:00
|
|
|
Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page.
|
|
|
|
|
2016-10-20 20:30:53 +02:00
|
|
|
### Form `application/x-www-form-urlencoded`
|
|
|
|
|
|
|
|
`POST` `/save`
|
|
|
|
|
|
|
|
name | value
|
|
|
|
:--- | :---
|
|
|
|
name | Joe Smith
|
|
|
|
email | joe@labstack.com
|
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
|
2016-10-20 20:30:53 +02:00
|
|
|
```go
|
2016-11-08 20:19:30 +02:00
|
|
|
// e.POST("/save", save)
|
2016-10-20 20:30:53 +02:00
|
|
|
func save(c echo.Context) error {
|
|
|
|
// Get name and email
|
|
|
|
name := c.FormValue("name")
|
|
|
|
email := c.FormValue("email")
|
2016-11-01 17:33:56 +02:00
|
|
|
return c.String(http.StatusOK, "name:" + name + ", email:" + email)
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
Run the following command:
|
|
|
|
|
2016-11-01 17:33:56 +02:00
|
|
|
```sh
|
|
|
|
$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
|
|
|
|
// => name:Joe Smith, email:joe@labstack.com
|
|
|
|
```
|
|
|
|
|
2016-10-20 20:30:53 +02:00
|
|
|
### Form `multipart/form-data`
|
|
|
|
|
|
|
|
`POST` `/save`
|
|
|
|
|
|
|
|
name | value
|
|
|
|
:--- | :---
|
|
|
|
name | Joe Smith
|
|
|
|
avatar | avatar
|
|
|
|
|
|
|
|
```go
|
|
|
|
func save(c echo.Context) error {
|
2016-11-01 17:33:56 +02:00
|
|
|
// Get name
|
2016-10-20 20:30:53 +02:00
|
|
|
name := c.FormValue("name")
|
|
|
|
// Get avatar
|
2016-11-08 20:19:30 +02:00
|
|
|
avatar, err := c.FormFile("avatar")
|
2016-11-01 17:33:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source
|
|
|
|
src, err := avatar.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
// Destination
|
|
|
|
dst, err := os.Create(avatar.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.HTML(http.StatusOK, "<b>Thank you! " + name + "</b>")
|
|
|
|
}
|
|
|
|
```
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-11-01 17:33:56 +02:00
|
|
|
Run the following command.
|
|
|
|
```sh
|
|
|
|
$ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/save
|
|
|
|
// => <b>Thank you! Joe Smith</b>
|
|
|
|
```
|
2016-11-08 20:19:30 +02:00
|
|
|
|
|
|
|
For checking uploaded image, run the following command.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-11-01 17:33:56 +02:00
|
|
|
```sh
|
|
|
|
cd <project directory>
|
|
|
|
ls avatar.png
|
|
|
|
// => avatar.png
|
2016-10-20 20:30:53 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### Handling Request
|
|
|
|
|
2016-12-15 21:08:56 +02:00
|
|
|
- Bind `json`, `xml`, `form` or `query` payload into Go struct based on `Content-Type`
|
|
|
|
request header.
|
|
|
|
- Render response as `json` or `xml` with status code.
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
type User struct {
|
2016-12-15 20:23:14 +02:00
|
|
|
Name string `json:"name" xml:"name" form:"name" query:"name"`
|
|
|
|
Email string `json:"email" xml:"email" form:"email" query:"name"`
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e.POST("/users", func(c echo.Context) error {
|
|
|
|
u := new(User)
|
|
|
|
if err := c.Bind(u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, u)
|
|
|
|
// or
|
|
|
|
// return c.XML(http.StatusCreated, u)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Static Content
|
|
|
|
|
|
|
|
Server any file from static directory for path `/static/*`.
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Static("/static", "static")
|
|
|
|
```
|
|
|
|
|
2016-11-25 23:03:03 +02:00
|
|
|
#### [Learn More](/guide/static-files)
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-11-25 23:03:03 +02:00
|
|
|
### [Template Rendering](/guide/templates)
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
### Middleware
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Root level middleware
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
|
|
|
|
// Group level middleware
|
|
|
|
g := e.Group("/admin")
|
|
|
|
g.Use(middleware.BasicAuth(func(username, password string) bool {
|
|
|
|
if username == "joe" && password == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Route level middleware
|
|
|
|
track := func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
println("request to /users")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.GET("/users", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "/users")
|
|
|
|
}, track)
|
|
|
|
```
|
2016-11-25 23:03:03 +02:00
|
|
|
|
|
|
|
#### [Learn More](/middleware)
|