Skip to content

Commit

Permalink
补充Swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Oct 7, 2024
1 parent 74b3c66 commit 6f53761
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
39 changes: 33 additions & 6 deletions ioc/apps/apidoc/restful/swagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package restful

import (
"fmt"

restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/v2/ioc"
Expand All @@ -14,7 +16,7 @@ import (
func init() {
ioc.Api().Registry(&SwaggerApiDoc{
ApiDoc: apidoc.ApiDoc{
Path: "/apidocs.json",
Path: "/apidocs",
},
})
}
Expand Down Expand Up @@ -47,20 +49,45 @@ func (h *SwaggerApiDoc) Meta() ioc.ObjectMeta {
return meta
}

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

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

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

ws := gorestful.ObjectRouter(h)
ws.Route(ws.GET("/").To(func(r *restful.Request, w *restful.Response) {
swagger := restfulspec.BuildSwagger(h.SwaggerDocConfig())
w.WriteAsJson(swagger)
}),
ws.Route(ws.GET("/swagger.json").To(h.SwaggerApiDoc).
Doc("Swagger JSON").
Metadata(restfulspec.KeyOpenAPITags, tags),
)
h.log.Info().Msgf("Get the API Doc using %s", h.ApiDocPath())

ws.Route(ws.GET("/ui.html").To(h.SwaggerUI).
Doc("Swagger UI").
Metadata(restfulspec.KeyOpenAPITags, tags),
)
h.log.Info().Msgf("Get the API UI using %s", h.ApiUIPath())
}

func (h *SwaggerApiDoc) SwaggerApiDoc(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))
func (h *SwaggerApiDoc) SwaggerUI(r *restful.Request, w *restful.Response) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(fmt.Sprintf(apidoc.HTML_REDOC, h.ApiDocPath())))
}

// API Doc
func (h *SwaggerApiDoc) SwaggerDocConfig() restfulspec.Config {
return restfulspec.Config{
Host: http.Get().Host,
WebServices: restful.RegisteredWebServices(),
APIPath: http.Get().ApiObjectPathPrefix(h),
PostBuildSwaggerObjectHandler: http.Get().SwagerDocs,
Expand Down
26 changes: 26 additions & 0 deletions ioc/apps/apidoc/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package apidoc

const HTML_REDOC = `<!DOCTYPE html>
<html>
<head>
<title>Redoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<!--
Redoc doesn't change outer page styles
-->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='%s'></redoc>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
</html>`

0 comments on commit 6f53761

Please sign in to comment.