Skip to content

changes README's hello-world example for v3 #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ package main

import (
"net/http"

"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)

func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Run(standard.New(":1323"))

if err := e.Start(":1323"); err != nil {
e.Logger.Fatal(err.Error())
}
}
```

Expand Down
82 changes: 55 additions & 27 deletions website/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func main() {
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Run(standard.New(":1323"))

if err := e.Start(":1323"); err != nil {
e.Logger.Fatal(err.Error())
}
}
```

Expand Down Expand Up @@ -85,8 +88,10 @@ e.DELETE("/users/:id", deleteUser)
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
return c.String(http.StatusOK, id)
}
```
Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.

### Query Parameters

Expand All @@ -97,9 +102,13 @@ func show(c echo.Context) error {
// Get team and member from the query string
team := c.QueryParam("team")
member := c.QueryParam("member")

return c.String(http.StatusOK, "team:" + team + ", member:" + member)
}
```

Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page.

### Form `application/x-www-form-urlencoded`

`POST` `/save`
Expand All @@ -114,51 +123,70 @@ func save(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormValue("email")

return c.String(http.StatusOK, "name:" + name + ", email:" + email)
}
```

Run the following command.
```sh
$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
// => name:Joe Smith, email:joe@labstack.com
```

### Form `multipart/form-data`

`POST` `/save`

name | value
:--- | :---
name | Joe Smith
email | joe@labstack.com
avatar | avatar

```go
func save(c echo.Context) error {
// Get name and email
// Get name
name := c.FormValue("name")
email := c.FormValue("email")
// Get avatar
avatar, err := c.FormFile("avatar")
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()
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>")
}
```

// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
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>
```

return c.HTML(http.StatusOK, "<b>Thank you!</b>")
}
To check the uploaded image, run the following command.
```sh
cd <project directory>
ls avatar.png
// => avatar.png
```

### Handling Request
Expand Down