-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_info.go
313 lines (273 loc) · 7.81 KB
/
api_info.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package pick
import (
"context"
"errors"
"github.com/hopeio/utils/log"
"net/http"
"reflect"
"strings"
"unsafe"
)
const Template = `
func (*UserService) Add(ctx *model.Context, req *model.SignupReq) (*response.TinyRep, error) {
pick.Api(func() {
pick.Post("").
Title("用户注册").
Version(2).
CreateLog("1.0.0", "jyb", "2019/12/16", "创建").
ChangeLog("2.0.1", "jyb", "2019/12/16", "修改测试").End()
})
return &response.TinyRep{Msg: req.Name}, nil
}
`
var (
ContextType = reflect.TypeOf((*context.Context)(nil)).Elem()
ContextValue = reflect.ValueOf(context.Background())
ErrorType = reflect.TypeOf((*error)(nil)).Elem()
)
type url struct {
Path, Method, Remark string
}
func Get(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodGet}}}
}
func Post(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodPost}}}
}
func Put(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodPut}}}
}
func Delete(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodDelete}}}
}
func Patch(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodPatch}}}
}
func Trace(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodTrace}}}
}
func Head(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodHead}}}
}
func Options(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodOptions}}}
}
func Connect(path string) *apiInfo {
return &apiInfo{urls: []url{{Path: path, Method: http.MethodConnect}}}
}
type apiInfo struct {
urls []url
title string
version int
changelog []changelog
createlog changelog
deprecated *changelog
}
type changelog struct {
Version, Auth, Date, Log string
}
func (api *apiInfo) Get(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodGet})
return api
}
func (api *apiInfo) GetRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodGet, Remark: remark})
return api
}
func (api *apiInfo) Post(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodPost})
return api
}
func (api *apiInfo) PostRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodPost, Remark: remark})
return api
}
func (api *apiInfo) Put(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodPut})
return api
}
func (api *apiInfo) PutRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodPut, Remark: remark})
return api
}
func (api *apiInfo) Delete(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodDelete})
return api
}
func (api *apiInfo) DeleteRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodDelete, Remark: remark})
return api
}
func (api *apiInfo) Patch(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodPatch})
return api
}
func (api *apiInfo) PatchRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodPatch, Remark: remark})
return api
}
func (api *apiInfo) Trace(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodTrace})
return api
}
func (api *apiInfo) TraceRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodTrace, Remark: remark})
return api
}
func (api *apiInfo) Head(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodHead})
return api
}
func (api *apiInfo) HeadRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodHead, Remark: remark})
return api
}
func (api *apiInfo) Options(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodOptions})
return api
}
func (api *apiInfo) OptionsRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodOptions, Remark: remark})
return api
}
func (api *apiInfo) Connect(path string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodConnect})
return api
}
func (api *apiInfo) ConnectRemark(path, remark string) *apiInfo {
api.urls = append(api.urls, url{Path: path, Method: http.MethodConnect, Remark: remark})
return api
}
func (api *apiInfo) ChangeLog(v, auth, date, log string) *apiInfo {
v = version(v)
api.changelog = append(api.changelog, changelog{v, auth, date, log})
return api
}
func version(v string) string {
if v[0] != 'v' {
return "v" + v
}
return v
}
func (api *apiInfo) CreateLog(v, auth, date, log string) *apiInfo {
if api.createlog.Version != "" {
panic("创建记录只允许一条")
}
v = version(v)
api.createlog = changelog{v, auth, date, log}
return api
}
func (api *apiInfo) Title(d string) *apiInfo {
api.title = d
return api
}
func (api *apiInfo) Version(v int) *apiInfo {
api.version = v
return api
}
func (api *apiInfo) Deprecated(v, auth, date, log string) *apiInfo {
v = version(v)
api.deprecated = &changelog{v, auth, date, log}
return api
}
func (api *apiInfo) End() {
panic(api)
}
func (api *apiInfo) Check() error {
if len(api.urls) == 0 || api.urls[0].Path == "" || api.urls[0].Method == "" || api.title == "" {
return errors.New("接口路径,方法,描述均为必填")
}
return nil
}
func (api *apiInfo) Log() {
for _, url := range api.urls {
Log(url.Method, url.Path, api.title)
}
}
func (api *apiInfo) Export() *ApiInfo {
return (*ApiInfo)(unsafe.Pointer(api))
}
type ApiInfo struct {
Urls []url
Title string
Version int
Changelog []changelog
Createlog changelog
Deprecated *changelog
}
// 获取负责人
func (api *ApiInfo) GetPrincipal() string {
if len(api.Changelog) == 0 {
return api.Createlog.Auth
}
if api.Deprecated != nil {
return api.Deprecated.Auth
}
return api.Changelog[len(api.Changelog)-1].Auth
}
// recover捕捉panic info
func GetMethodInfo(method *reflect.Method, preUrl string, httpContext reflect.Type) (info *apiInfo) {
if method.Name == "Service" {
return nil
}
if !strings.HasPrefix(method.Name, prefix) {
return nil
}
defer func() {
if err := recover(); err != nil {
if v, ok := err.(*apiInfo); ok {
//_,_, info.Version = ParseMethodName(Method.Name)
if v.version == 0 {
v.version = 1
}
for i := range v.urls {
v.urls[i].Path = preUrl + v.urls[i].Path
}
info = v
} else {
log.Error(err)
}
}
}()
methodValue := method.Func
methodType := methodValue.Type()
numIn := methodType.NumIn()
numOut := methodType.NumOut()
var err error
defer func() {
if err != nil {
log.Debugf("未注册: %s 原因:%v", method.Name, err)
}
}()
if numIn != 3 {
//err = errors.New("method参数必须为两个")
return
}
if numOut != 2 {
//err = errors.New("method返回值必须为两个")
return
}
if !methodType.In(1).ConvertibleTo(httpContext) && !methodType.In(1).Implements(ContextType) {
err = errors.New("service第一个参数必须为*httpctx.Context类型或context.Context")
return
}
if !methodType.Out(1).Implements(ErrorType) {
err = errors.New("service第二个返回值必须为error类型")
return
}
params := make([]reflect.Value, numIn)
params[0] = reflect.New(methodType.In(0).Elem())
if methodType.In(1).ConvertibleTo(httpContext) {
params[1] = reflect.New(methodType.In(1).Elem())
} else {
params[1] = ContextValue
}
params[2] = reflect.New(methodType.In(2).Elem())
methodValue.Call(params)
return nil
}