-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
41 lines (30 loc) · 771 Bytes
/
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
package main
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/songzhaoliang/echotool"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
r := echo.New()
r.Use(echotool.SetRequestID(echotool.GetUUID))
e := echotool.NewEngine()
e.Use(CheckToken)
r.POST("/users", e.EchoHandler(CreateUser))
r.Start(":1323")
}
func CheckToken(c echo.Context, ec *echotool.Context) {
token := echotool.MustHeaderString(c, "X-Token")
if token != "RightToken" {
ec.Abort(echotool.CodeForbidden, fmt.Errorf("invalid token"))
}
}
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)
}