-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_test.go
188 lines (173 loc) · 5 KB
/
gin_test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package web
import (
"fmt"
"runtime"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/go-pay/ecode"
"github.com/go-pay/web/metadata"
"github.com/go-pay/web/middleware"
"github.com/go-pay/xlog"
)
type MemStats struct {
Alloc string `json:"alloc"`
TotalAlloc string `json:"total_alloc"`
Sys string `json:"sys"`
HeapAlloc string `json:"heap_alloc"`
HeapSys string `json:"heap_sys"`
HeapIdle string `json:"heap_idle"`
HeapInuse string `json:"heap_inuse"`
HeapReleased string `json:"heap_released"`
Frees string `json:"frees"`
StackInuse string `json:"stack_inuse"`
StackSys string `json:"stack_sys"`
GcSys string `json:"gc_sys"`
NextGc string `json:"next_gc"`
LastGc string `json:"last_gc"`
NumGc int `json:"num_gc"`
NumForcedGc int `json:"num_forced_gc"`
EnableGc bool `json:"enable_gc"`
}
func TestInitServer(t *testing.T) {
// 需要测试请自行解开注释测试
//c := &Config{
// Addr: ":2233",
// Debug: true,
// ReadTimeout: xtime.Duration(15 * time.Second),
// WriteTimeout: xtime.Duration(15 * time.Second),
// Limiter: &limiter.Config{
// Rate: 0, // 0 速率不限流
// BucketSize: 100,
// },
//}
//
//g := InitGin(c)
////g.Gin.Use(middleware.CORS())
//// 配置自定义监控的header
////middleware.SetAccessLogHeader([]string{"content-type", "sign"})
//// 默认header新增
//middleware.AddAccessLogHeader([]string{"content-type", "sign"})
//
//xlog.Level = xlog.DebugLevel
//ecode.Success = ecode.New(0, "SUCCESS", "成功")
//initRoute(g.Gin)
//
//// add hook
//g.AddShutdownHook(func(c context.Context) {
// sec := 0
// ticker := time.NewTicker(time.Second * 1)
// defer ticker.Stop()
// for {
// <-ticker.C
// sec++
// xlog.Warnf("second: %ds", sec)
// }
//}).AddExitHook(func(c context.Context) {
// xlog.Warn("after close hook1")
//}, func(c context.Context) {
// xlog.Warn("after close hook2")
//}).Start()
}
func initRoute(g *gin.Engine) {
g.GET("/a/:abc", func(c *gin.Context) {
xlog.Debug(c.Param("abc"))
xlog.Debug(c.Request.RequestURI)
rsp := &struct {
Param string `json:"param"`
Path string `json:"path"`
}{Param: c.Param("abc"), Path: c.Request.RequestURI}
JSON(c, rsp, nil)
})
g.GET("/b", func(c *gin.Context) {
JSON(c, nil, ecode.UnauthorizedErr)
})
group := g.Group("/group", middleware.AccessLog("test-app"))
{
group.POST("/c", func(c *gin.Context) {
body, err := metadata.RequestBody(c.Request)
if err != nil {
xlog.Error(err)
JSON(c, nil, err)
return
}
xlog.Debugf("body:%s", body)
var ss = struct {
Name string `json:"name"`
}{}
_ = c.ShouldBindJSON(&ss)
JSON(c, ss, nil)
})
}
g.GET("/d", func(c *gin.Context) {
JSON(c, Pager{PageNo: 1, PageSize: 15}.Apply(30, "我是15条数据"), nil)
})
g.POST("/wechatCallback", func(c *gin.Context) {
//notify, err := wechat.V3ParseNotify(c.Request)
//if err != nil {
// xlog.Errorf("wechat.V3ParseNotify(),err:%+v", err)
// return
//}
//xlog.Debug("Id:", notify.Id)
//xlog.Debug("EventType:", notify.EventType)
//xlog.Debug("ResourceType:", notify.ResourceType)
//xlog.Debug("Resource:", notify.Resource)
//xlog.Debug("CreateTime:", notify.CreateTime)
//xlog.Debug("Summary:", notify.Summary)
})
// proxy
g.GET("/gopher/web/memStats", memStats)
// postman request: GET http://localhost:2233/proxy/a
g.GET("/proxy/a", func(c *gin.Context) {
rsp, err := middleware.GinProxy[*MemStats](c, "http://localhost:2233", "/gopher/web/memStats")
if err != nil {
xlog.Errorf("GinProxy err: %v", err)
JSON(c, nil, err)
return
}
JSON(c, rsp, nil)
})
g.GET("/app/ping", func(c *gin.Context) {
middleware.GinPureProxy(c, "http://localhost:1122")
})
// postman request: POST http://localhost:2233/gopher/web/memStats
g.POST("/gopher/web/memStats", func(c *gin.Context) {
rsp, err := middleware.GinProxy[*MemStats](c, "http://localhost:2233", "")
if err != nil {
xlog.Errorf("GinProxy err: %v", err)
JSON(c, nil, err)
return
}
JSON(c, rsp, nil)
})
}
func memStats(c *gin.Context) {
ms := &runtime.MemStats{}
runtime.ReadMemStats(ms)
rsp := &struct {
HeapAlloc string
HeapIdle string
HeapInuse string
HeapReleased string
Frees string
GCSys string
NextGC string
LastGC string
NumGC uint32
NumForcedGC uint32
EnableGC bool
}{
HeapAlloc: fmt.Sprintf("%d(MB)", ms.HeapAlloc/1024/1024),
HeapIdle: fmt.Sprintf("%d(MB)", ms.HeapIdle/1024/1024),
HeapInuse: fmt.Sprintf("%d(MB)", ms.HeapInuse/1024/1024),
HeapReleased: fmt.Sprintf("%d(MB)", ms.HeapReleased/1024/1024),
Frees: fmt.Sprintf("%d(MB)", ms.Frees/1024/1024),
GCSys: fmt.Sprintf("%d(MB)", ms.GCSys/1024/1024),
NextGC: fmt.Sprintf("%d(MB)", ms.NextGC/1024/1024),
LastGC: time.Unix(0, int64(ms.LastGC)).Format("2006-01-02 15:04:05.000"),
NumGC: ms.NumGC,
NumForcedGC: ms.NumForcedGC,
EnableGC: ms.EnableGC,
}
JSON(c, rsp, nil)
}