Skip to content

Commit

Permalink
补充API Doc UI
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Oct 7, 2024
1 parent e9c964b commit ed47133
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
16 changes: 14 additions & 2 deletions ioc/apps/apidoc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ const (
AppName = "apidocs"
)

func DefaultApiDoc() *ApiDoc {
return &ApiDoc{
BasePath: "",
JsonPath: "/swagger.json",
UIPath: "/ui.html",
}
}

type ApiDoc struct {
// Swagger API Doc URL路径
Path string `json:"path" yaml:"path" toml:"path" env:"HTTP_API_DOC_PATH"`
// Swagger API Doc BASE API URL路径
BasePath string `json:"base_path" yaml:"base_path" toml:"base_path" env:"BASE_PATH"`
// Swagger JSON API path
JsonPath string `json:"json_path" yaml:"json_path" toml:"json_path" env:"JSON_PATH"`
// Swagger UI path
UIPath string `json:"ui_path" yaml:"ui_path" toml:"ui_path" env:"UI_PATH"`
}
18 changes: 9 additions & 9 deletions ioc/apps/apidoc/restful/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func init() {
ioc.Api().Registry(&SwaggerApiDoc{
ApiDoc: apidoc.ApiDoc{},
ApiDoc: apidoc.DefaultApiDoc(),
})
}

Expand All @@ -24,7 +24,7 @@ type SwaggerApiDoc struct {
ioc.ObjectImpl
log *zerolog.Logger

apidoc.ApiDoc
*apidoc.ApiDoc
}

func (h *SwaggerApiDoc) Name() string {
Expand All @@ -43,31 +43,31 @@ func (i *SwaggerApiDoc) Priority() int {

func (h *SwaggerApiDoc) Meta() ioc.ObjectMeta {
meta := ioc.DefaultObjectMeta()
if h.Path != "" {
meta.CustomPathPrefix = h.Path
if h.BasePath != "" {
meta.CustomPathPrefix = h.BasePath
}
return meta
}

func (h *SwaggerApiDoc) ApiDocPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), "/swagger.json")
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.JsonPath)
}

func (h *SwaggerApiDoc) ApiUIPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), "/ui.html")
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.UIPath)
}

func (h *SwaggerApiDoc) Registry() {
tags := []string{"API 文档"}

ws := gorestful.ObjectRouter(h)
ws.Route(ws.GET("/swagger.json").To(h.SwaggerApiDoc).
ws.Route(ws.GET(h.JsonPath).To(h.SwaggerApiDoc).
Doc("Swagger JSON").
Metadata(restfulspec.KeyOpenAPITags, tags),
)
h.log.Info().Msgf("Get the API Doc using %s", h.ApiDocPath())
h.log.Info().Msgf("Get the API JSON data using %s", h.ApiDocPath())

ws.Route(ws.GET("/ui.html").To(h.SwaggerUI).
ws.Route(ws.GET(h.UIPath).To(h.SwaggerUI).
Doc("Swagger UI").
Metadata(restfulspec.KeyOpenAPITags, tags),
)
Expand Down
35 changes: 28 additions & 7 deletions ioc/apps/apidoc/swaggo/swagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package swaggo

import (
"fmt"

"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/apps/apidoc"
Expand All @@ -13,7 +15,7 @@ import (

func init() {
ioc.Api().Registry(&SwaggerApiDoc{
ApiDoc: apidoc.ApiDoc{},
ApiDoc: apidoc.DefaultApiDoc(),
InstanceName: "swagger",
})
}
Expand All @@ -22,7 +24,7 @@ type SwaggerApiDoc struct {
ioc.ObjectImpl
log *zerolog.Logger

apidoc.ApiDoc
*apidoc.ApiDoc
InstanceName string `json:"instance_name" yaml:"instance_name" toml:"instance_name" env:"SWAGGER_INSTANCE_NAME"`
}

Expand All @@ -42,15 +44,34 @@ func (i *SwaggerApiDoc) Priority() int {

func (h *SwaggerApiDoc) Meta() ioc.ObjectMeta {
meta := ioc.DefaultObjectMeta()
meta.CustomPathPrefix = h.BasePath
if h.BasePath != "" {
meta.CustomPathPrefix = h.BasePath
}
return meta
}

func (h *SwaggerApiDoc) Registry() {
r := ioc_gin.ObjectRouter(h)
r.GET("/", func(c *gin.Context) {
c.Writer.WriteString(swag.GetSwagger(h.InstanceName).ReadDoc())
})
r.GET(h.JsonPath, h.SwaggerJson)
h.log.Info().Msgf("Get the API JSON data using %s", h.ApiDocPath())

r.GET(h.UIPath, h.SwaggerUI)
h.log.Info().Msgf("Get the API UI using %s", h.ApiUIPath())
}

func (h *SwaggerApiDoc) ApiDocPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.JsonPath)
}

func (h *SwaggerApiDoc) ApiUIPath() string {
return fmt.Sprintf("%s%s", http.Get().ApiObjectAddr(h), h.UIPath)
}

func (h *SwaggerApiDoc) SwaggerJson(ctx *gin.Context) {
ctx.Writer.WriteString(swag.GetSwagger(h.InstanceName).ReadDoc())
}

h.log.Info().Msgf("Get the API Doc using %s", http.Get().ApiObjectAddr(h))
func (h *SwaggerApiDoc) SwaggerUI(ctx *gin.Context) {
ctx.Writer.Header().Set("Content-Type", "text/html")
ctx.Writer.WriteString(fmt.Sprintf(apidoc.HTML_REDOC, h.ApiDocPath()))
}

0 comments on commit ed47133

Please sign in to comment.