-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (54 loc) · 1.4 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
package main
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/songzhaoliang/echotool"
)
type Response struct {
CodeN int `json:"CodeN"`
Code string `json:"Code"`
Data interface{} `json:"Data,omitempty"`
}
func RespOK(code int, data interface{}) *Response {
return &Response{
CodeN: code,
Code: echotool.CodeMsg(code),
Data: data,
}
}
func RespError(code int, err error) *Response {
return &Response{
CodeN: code,
Code: fmt.Sprintf("%s - %v", echotool.CodeMsg(code), err),
}
}
func NewEngine() *echotool.Engine {
opts := []echotool.Option{
echotool.WithFinisher(func(c echo.Context, ec *echotool.Context) {
code, data := ec.GetCode(), ec.GetData()
c.JSON(echotool.HTTPStatus(code), RespOK(code, data))
}),
echotool.WithAborter(func(c echo.Context, ec *echotool.Context) {
code, err := ec.GetCode(), ec.GetError()
fmt.Printf("%s - %v\n", echotool.CodeMsg(code), err)
c.JSON(echotool.HTTPStatus(code), RespError(code, err))
}),
}
return echotool.NewEngine(opts...)
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
r := echo.New()
e := NewEngine()
r.POST("/users", e.EchoHandler(CreateUser))
r.Start(":1323")
}
func CreateUser(c echo.Context, ec *echotool.Context) {
user := &User{}
echotool.New(c, user).JSONBindBody().MustEnd()
fmt.Printf("user is %+v\n", user)
ec.Finish(echotool.CodeOKZero, nil)
}