/
handler.go
97 lines (78 loc) · 2.14 KB
/
handler.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
package example
import (
"github.com/EndlessCheng/echo-autodoc"
"github.com/labstack/echo"
"net/http"
"time"
"io/ioutil"
"strconv"
)
// This is the example.
func runHTTPServer() error {
e := echo.New()
setHTTPHandler(autodoc.NewGroup(e)) // 原代码:setHTTPHandler(e)
return e.Start(":8008")
}
func setHTTPHandler(g autodoc.GroupInterface) { // 原代码:*echo.Echo
h := &handler{}
// [skip gen]
g.GET("/", h.index)
api := g.Group("/api")
// 上传一本书
api.POST("/upload_book", h.uploadBook)
// 获取一本书的信息
// 相关出版社有:
// - 机械工业出版社
// - 电子工业出版社
// - 人民邮电出版社
api.GET("/book", h.getBook)
// 更新一本书的信息
// POST 时注意作者需要存到一个数组中
api.GET("/update_book", h.updateBook)
}
type handler struct {
}
func (h *handler) index(c echo.Context) error {
return c.String(http.StatusOK, JSONTime(time.Now()).String())
}
func (h *handler) uploadBook(c echo.Context) error {
fh, err := c.FormFile("file")
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
f, err := fh.Open()
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
fileName := c.FormValue("file_name") // 文件名
if fileName == "" {
fileName = fh.Filename
}
deltaStr := c.FormValue("delta") // int, 偏移量
_, err = strconv.Atoi(deltaStr)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, &struct {
Content string `json:"content" desc:"上传的内容"`
}{string(content)})
}
func (h *handler) getBook(c echo.Context) error {
c.QueryParam("isbn") // 书的 ISBN
return c.JSON(http.StatusOK, &exampleBook)
}
func (h *handler) updateBook(c echo.Context) error {
d := Book{}
if err := c.Bind(&d); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, &struct {
ErrorCode int `json:"error_code" desc:"错误码"`
ErrorMsg string `json:"error_msg" desc:"错误信息"`
}{1000, "OK"})
}