-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (84 loc) · 1.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"io"
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello World!")
})
e.GET("/users/:id", getUser)
e.GET("/show", showuser)
e.POST("/form", formSave)
e.POST("/formData", formData)
e.POST("/users", users)
// Start server
e.Logger.Fatal(e.Start(":1323"))
}
// URL 路徑參數
func getUser(c echo.Context) error {
// User ID 來自url users:/:id
id := c.Param("id")
return c.String(http.StatusOK, id)
}
// 請求參數
func showuser(c echo.Context) error {
// 從請求參數 user 和 phone 的值
user := c.QueryParam("user")
phone := c.QueryParam("phone")
return c.String(http.StatusOK, "user:"+user+" \nphone:"+phone)
}
// 表單 application/x-www-form-urlencoded
func formSave(c echo.Context) error {
name := c.FormValue("name")
email := c.FormValue("email")
return c.String(http.StatusOK, "name:"+name+"\nemail:"+email)
}
// 表單 multipart/form-data
func formData(c echo.Context) error {
// Get name
name := c.FormValue("name")
// 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()
// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
return c.HTML(http.StatusOK, "<p>Thank you! "+name+"</p>")
}
type User struct {
Name string `json:"name" xml:"name" form:"name" query:"name"`
Email string `json:"email" xml:"email" form:"email" query:"email"`
}
func users(c echo.Context) error {
u := new(User)
if err := c.Bind(u); err != nil {
return err
}
return c.JSON(http.StatusCreated, u)
// c.XML(http.StatusCreated, u)
}