-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (64 loc) · 1.55 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
package main
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/songzhaoliang/echotool"
)
const (
CodeOracleErr = 50018
)
var codeMsg = map[int]string{
CodeOracleErr: "oracle error",
}
func init() {
echotool.RegisterCode(CodeOracleErr, codeMsg[CodeOracleErr], http.StatusInternalServerError)
}
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()
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()
MustReserveQuota()
cbs := []echotool.CallbackFunc{func(error) {
RollbackQuota()
}}
MustOracleCreateUser(user, cbs...)
ec.Finish(echotool.CodeOKZero, nil)
}
func OracleCreateUser(user *User, cbs ...echotool.CallbackFunc) error {
return fmt.Errorf("duplicate entry")
}
func MustOracleCreateUser(user *User, cbs ...echotool.CallbackFunc) {
echotool.MustDoCallback(func() (data interface{}, err error) {
if err = OracleCreateUser(user); err != nil {
fmt.Printf("OracleCreateUser error - %v\n", err)
}
return
}, CodeOracleErr, cbs...)
}
func ReserveQuota() error {
fmt.Println("reserve quota")
return nil
}
func MustReserveQuota() {
echotool.MustDo(func() (data interface{}, err error) {
if err = ReserveQuota(); err != nil {
fmt.Printf("ReserveQuota error - %v\n", err)
}
return
})
}
func RollbackQuota() error {
fmt.Println("rollback quota")
return nil
}