-
Notifications
You must be signed in to change notification settings - Fork 46
/
swagger.go
74 lines (63 loc) · 1.77 KB
/
swagger.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
package restful
import (
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/apps/apidoc"
"github.com/infraboard/mcube/v2/ioc/config/gorestful"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
"github.com/rs/zerolog"
)
func init() {
ioc.Api().Registry(&SwaggerApiDoc{
ApiDoc: apidoc.ApiDoc{
Path: "/apidocs.json",
},
})
}
// 等待所有的API接口都加载到Router上后, 提取出所有的接口并生产API Doc
type SwaggerApiDoc struct {
ioc.ObjectImpl
log *zerolog.Logger
apidoc.ApiDoc
}
func (h *SwaggerApiDoc) Name() string {
return apidoc.AppName
}
func (h *SwaggerApiDoc) Init() error {
h.log = log.Sub("api_doc")
h.Registry()
return nil
}
func (i *SwaggerApiDoc) Priority() int {
return -100
}
func (h *SwaggerApiDoc) Meta() ioc.ObjectMeta {
meta := ioc.DefaultObjectMeta()
meta.CustomPathPrefix = h.Path
return meta
}
func (h *SwaggerApiDoc) Registry() {
ws := gorestful.ObjectRouter(h)
ws.Route(ws.GET("/").To(func(r *restful.Request, w *restful.Response) {
swagger := restfulspec.BuildSwagger(h.SwaggerDocConfig())
w.WriteAsJson(swagger)
}),
)
h.log.Info().Msgf("Get the API Doc using %s", http.Get().ApiObjectAddr(h))
}
// API Doc
func (h *SwaggerApiDoc) SwaggerDocConfig() restfulspec.Config {
return restfulspec.Config{
WebServices: restful.RegisteredWebServices(),
APIPath: http.Get().ApiObjectPathPrefix(h),
PostBuildSwaggerObjectHandler: http.Get().SwagerDocs,
DefinitionNameHandler: func(name string) string {
if name == "state" || name == "sizeCache" || name == "unknownFields" {
return ""
}
return name
},
}
}