Skip to content

Commit 2cb612f

Browse files
Antonio Paganovishr
authored andcommitted
changes README's hello-world example for v3 (#698)
* [doc] adding graceful documentation and example. * adding myself to the maintainers list and minor comment formatting change * [doc] updating code on the guides/context.md and guides/cookies.md to use v3 code. * [doc] updating error-handling and request to v3 codebase * [doc] updating templates documentation * [doc] cleaning hello-world documentation for v3 * [content] adding website index content based on #667
1 parent f720ea1 commit 2cb612f

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,19 @@ package main
4545

4646
import (
4747
"net/http"
48+
4849
"github.com/labstack/echo"
49-
"github.com/labstack/echo/engine/standard"
5050
)
5151

5252
func main() {
5353
e := echo.New()
5454
e.GET("/", func(c echo.Context) error {
5555
return c.String(http.StatusOK, "Hello, World!")
5656
})
57-
e.Run(standard.New(":1323"))
57+
58+
if err := e.Start(":1323"); err != nil {
59+
e.Logger.Fatal(err.Error())
60+
}
5861
}
5962
```
6063

website/content/index.md

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ func main() {
5757
e.GET("/", func(c echo.Context) error {
5858
return c.String(http.StatusOK, "Hello, World!")
5959
})
60-
e.Run(standard.New(":1323"))
60+
61+
if err := e.Start(":1323"); err != nil {
62+
e.Logger.Fatal(err.Error())
63+
}
6164
}
6265
```
6366

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

9196
### Query Parameters
9297

@@ -97,9 +102,13 @@ func show(c echo.Context) error {
97102
// Get team and member from the query string
98103
team := c.QueryParam("team")
99104
member := c.QueryParam("member")
105+
106+
return c.String(http.StatusOK, "team:" + team + ", member:" + member)
100107
}
101108
```
102109

110+
Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page.
111+
103112
### Form `application/x-www-form-urlencoded`
104113

105114
`POST` `/save`
@@ -114,51 +123,70 @@ func save(c echo.Context) error {
114123
// Get name and email
115124
name := c.FormValue("name")
116125
email := c.FormValue("email")
126+
127+
return c.String(http.StatusOK, "name:" + name + ", email:" + email)
117128
}
118129
```
119130

131+
Run the following command.
132+
```sh
133+
$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
134+
// => name:Joe Smith, email:joe@labstack.com
135+
```
136+
120137
### Form `multipart/form-data`
121138

122139
`POST` `/save`
123140

124141
name | value
125142
:--- | :---
126143
name | Joe Smith
127-
email | joe@labstack.com
128144
avatar | avatar
129145

130146
```go
131147
func save(c echo.Context) error {
132-
// Get name and email
148+
// Get name
133149
name := c.FormValue("name")
134-
email := c.FormValue("email")
135150
// Get avatar
136151
avatar, err := c.FormFile("avatar")
137-
if err != nil {
138-
return err
139-
}
140-
141-
// Source
142-
src, err := avatar.Open()
143-
if err != nil {
144-
return err
145-
}
146-
defer src.Close()
147-
148-
// Destination
149-
dst, err := os.Create(avatar.Filename)
150-
if err != nil {
151-
return err
152-
}
153-
defer dst.Close()
152+
if err != nil {
153+
return err
154+
}
155+
156+
// Source
157+
src, err := avatar.Open()
158+
if err != nil {
159+
return err
160+
}
161+
defer src.Close()
162+
163+
// Destination
164+
dst, err := os.Create(avatar.Filename)
165+
if err != nil {
166+
return err
167+
}
168+
defer dst.Close()
169+
170+
// Copy
171+
if _, err = io.Copy(dst, src); err != nil {
172+
return err
173+
}
174+
175+
return c.HTML(http.StatusOK, "<b>Thank you! " + name + "</b>")
176+
}
177+
```
154178

155-
// Copy
156-
if _, err = io.Copy(dst, src); err != nil {
157-
return err
158-
}
179+
Run the following command.
180+
```sh
181+
$ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/save
182+
// => <b>Thank you! Joe Smith</b>
183+
```
159184

160-
return c.HTML(http.StatusOK, "<b>Thank you!</b>")
161-
}
185+
To check the uploaded image, run the following command.
186+
```sh
187+
cd <project directory>
188+
ls avatar.png
189+
// => avatar.png
162190
```
163191

164192
### Handling Request

0 commit comments

Comments
 (0)