forked from dtm-labs/dtm-cases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (71 loc) · 1.77 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
package main
import (
"fmt"
"log"
"time"
"github.com/dtm-labs/dtm-cases/order/common"
"github.com/dtm-labs/dtm-cases/order/conf"
"github.com/dtm-labs/dtm-cases/order/service"
"github.com/dtm-labs/dtm-cases/utils"
"github.com/dtm-labs/dtmcli"
"github.com/dtm-labs/dtmcli/logger"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql" // register mysql driver
"github.com/lithammer/shortuuid/v3"
)
func main() {
logger.InitLog("debug")
startSvr()
// fireRequest(defaultReq())
select {}
}
func startSvr() {
gin.SetMode(gin.ReleaseMode)
app := gin.Default()
addRoutes(app)
log.Printf("order examples listening at %d", conf.BusiPort)
go app.Run(fmt.Sprintf(":%d", conf.BusiPort))
time.Sleep(100 * time.Millisecond)
}
func addRoutes(app *gin.Engine) {
service.AddAPIRoute(app)
service.AddCouponRoute(app)
service.AddOrderRoute(app)
service.AddPayRoute(app)
service.AddStockRoute(app)
app.Any("/api/fireSucceed", utils.WrapHandler(func(c *gin.Context) interface{} {
req := defaultReq()
return fireRequest(req)
}))
app.Any("/api/fireFailed", utils.WrapHandler(func(c *gin.Context) interface{} {
req := defaultReq()
req.ProductCount = 1000
return fireRequest(req)
}))
app.Any("/api/fireFailedCoupon", utils.WrapHandler(func(c *gin.Context) interface{} {
req := defaultReq()
req.CouponID = 101
return fireRequest(req)
}))
}
func defaultReq() *common.Req {
return &common.Req{
UserID: 1,
OrderID: shortuuid.New(),
ProductID: 1,
ProductCount: 1,
CouponID: 0,
Amount: 100,
}
}
func fireRequest(req *common.Req) interface{} {
resty := dtmcli.GetRestyClient()
resp, err := resty.R().SetBody(req).Post(conf.BusiUrl + "/submitOrder")
if err != nil {
return err
}
if resp.IsError() {
return resp.Error()
}
return &req
}