feat: 整合swagger-ui文档主题, 并优化文档页面渲染
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -46,21 +47,23 @@ func (su *SwaggerUI) Handler() func(ctx *gin.Context) {
|
||||
// YDoc-Lucky-UI 主题处理
|
||||
return su.HandleLuckyUI()
|
||||
case consts.SwaggerUIThemeDefault:
|
||||
return su.HandleSwaggerUI()
|
||||
return su.HandleSwaggerDefaultUI()
|
||||
case consts.SwaggerUIThemeRedocFree:
|
||||
// redoc免费版
|
||||
return su.HandleRedocFreeUI()
|
||||
case consts.SwaggerUIThemeElements:
|
||||
return su.HandleElements()
|
||||
return su.renderWebPage(theme.ElementsFiles, su.uiTheme)
|
||||
case consts.SwaggerUIThemeSwaggerUI:
|
||||
return su.renderWebPage(theme.SwaggerUIFiles, su.uiTheme)
|
||||
default:
|
||||
return su.HandleSwaggerUI()
|
||||
return su.HandleSwaggerDefaultUI()
|
||||
}
|
||||
}
|
||||
|
||||
func (su *SwaggerUI) HandleElements() gin.HandlerFunc {
|
||||
func (su *SwaggerUI) renderWebPage(themeFiles embed.FS, themeUI consts.SwaggerUITheme) func(ctx *gin.Context) {
|
||||
return func(ctx *gin.Context) {
|
||||
fileRealPath := strings.TrimPrefix(ctx.Request.RequestURI, su.baseUri)
|
||||
byteData, _ := theme.ElementsFiles.ReadFile(filepath.Join("elements", fileRealPath))
|
||||
byteData, _ := themeFiles.ReadFile(filepath.Join(themeUI.String(), fileRealPath))
|
||||
if strings.Contains(ctx.Request.RequestURI, ".html") {
|
||||
byteData = []byte(strings.ReplaceAll(string(byteData), "{{BASE_URI}}", su.baseUri))
|
||||
byteData = []byte(strings.ReplaceAll(string(byteData), "{{DOC_TITLE}}", su.docTitle))
|
||||
@@ -73,7 +76,7 @@ func (su *SwaggerUI) HandleElements() gin.HandlerFunc {
|
||||
uriArr := strings.Split(ctx.Request.RequestURI, ".")
|
||||
contentType := "text/" + uriArr[len(uriArr)-1]
|
||||
if strings.HasSuffix(ctx.Request.RequestURI, "png") {
|
||||
contentType = "image/png"
|
||||
contentType = consts.MimeTypeImagePng
|
||||
} else if strings.HasSuffix(ctx.Request.RequestURI, "js") {
|
||||
contentType = "application/javascript"
|
||||
}
|
||||
@@ -116,8 +119,8 @@ func (su *SwaggerUI) HandleKnife4goUI() func(ctx *gin.Context) {
|
||||
return knife4goGin.WrapHandler(knife4goFiles.Handler, resetOption)
|
||||
}
|
||||
|
||||
// HandleSwaggerUI ...
|
||||
func (su *SwaggerUI) HandleSwaggerUI() func(ctx *gin.Context) {
|
||||
// HandleSwaggerDefaultUI ...
|
||||
func (su *SwaggerUI) HandleSwaggerDefaultUI() func(ctx *gin.Context) {
|
||||
return ginSwagger.WrapHandler(swaggerFiles.Handler)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user