-
Notifications
You must be signed in to change notification settings - Fork 46
/
swagger.go
53 lines (43 loc) · 1.15 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
package swaggo
import (
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/apps/apidoc"
"github.com/infraboard/mcube/v2/ioc/config/http"
"github.com/infraboard/mcube/v2/ioc/config/log"
"github.com/rs/zerolog"
"github.com/swaggo/swag"
)
func init() {
ioc.Api().Registry(&SwaggerApiDoc{
ApiDoc: apidoc.ApiDoc{},
InstanceName: "swagger",
})
}
type SwaggerApiDoc struct {
ioc.ObjectImpl
log *zerolog.Logger
apidoc.ApiDoc
InstanceName string `json:"instance_name" yaml:"instance_name" toml:"instance_name" env:"SWAGGER_INSTANCE_NAME"`
}
func (h *SwaggerApiDoc) Name() string {
return apidoc.AppName
}
func (h *SwaggerApiDoc) Init() error {
h.log = log.Sub("api_doc")
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(r gin.IRouter) {
r.GET("/", func(c *gin.Context) {
c.Writer.WriteString(swag.GetSwagger(h.InstanceName).ReadDoc())
})
h.log.Info().Msgf("Get the API Doc using %s", http.Get().ApiObjectAddr(h))
}