优化代码处理
This commit is contained in:
parent
bd25ea3d96
commit
4ef680d917
@ -63,21 +63,6 @@ func (su *SwaggerUI) RegisterHandler(router *gin.Engine, baseUri string) {
|
|||||||
baseUri = "/docs/swagger"
|
baseUri = "/docs/swagger"
|
||||||
}
|
}
|
||||||
su.baseUri = baseUri
|
su.baseUri = baseUri
|
||||||
router.GET("/assets/*any", func(ctx *gin.Context) {
|
|
||||||
if su.uiTheme == define.SwaggerUIThemeYDocLucky {
|
|
||||||
fileRealPath := strings.TrimPrefix(ctx.Request.RequestURI, baseUri)
|
|
||||||
byteData, _ := ydocUIFiles.ReadFile(define.SwaggerUIThemeYDocLucky + fileRealPath)
|
|
||||||
uriArr := strings.Split(ctx.Request.RequestURI, ".")
|
|
||||||
contentType := "text/" + uriArr[len(uriArr)-1]
|
|
||||||
if strings.HasSuffix(ctx.Request.RequestURI, "png") {
|
|
||||||
contentType = "image/png"
|
|
||||||
}
|
|
||||||
ctx.Header("Content-Type", contentType)
|
|
||||||
ctx.String(http.StatusOK, string(byteData))
|
|
||||||
ctx.Abort()
|
|
||||||
}
|
|
||||||
ctx.AbortWithStatus(http.StatusNotFound)
|
|
||||||
})
|
|
||||||
router.GET(baseUri+"/*any", func(ctx *gin.Context) {
|
router.GET(baseUri+"/*any", func(ctx *gin.Context) {
|
||||||
if ctx.Request.RequestURI == baseUri+"/doc.json" {
|
if ctx.Request.RequestURI == baseUri+"/doc.json" {
|
||||||
// 默认swagger, 通过此接口读取文档数据
|
// 默认swagger, 通过此接口读取文档数据
|
||||||
@ -95,23 +80,6 @@ func (su *SwaggerUI) RegisterHandler(router *gin.Engine, baseUri string) {
|
|||||||
})
|
})
|
||||||
ctx.Abort()
|
ctx.Abort()
|
||||||
}
|
}
|
||||||
if su.uiTheme == define.SwaggerUIThemeYDocLucky {
|
|
||||||
fileRealPath := strings.TrimPrefix(ctx.Request.RequestURI, baseUri)
|
|
||||||
byteData, _ := ydocUIFiles.ReadFile(define.SwaggerUIThemeYDocLucky + fileRealPath)
|
|
||||||
if strings.HasSuffix(ctx.Request.RequestURI, "html") {
|
|
||||||
byteData = []byte(strings.ReplaceAll(string(byteData), "{{BASE_URI}}", baseUri))
|
|
||||||
}
|
|
||||||
uriArr := strings.Split(ctx.Request.RequestURI, ".")
|
|
||||||
contentType := "text/" + uriArr[len(uriArr)-1]
|
|
||||||
if strings.HasSuffix(ctx.Request.RequestURI, "png") {
|
|
||||||
contentType = "image/png"
|
|
||||||
} else if strings.HasSuffix(ctx.Request.RequestURI, "js") {
|
|
||||||
contentType = "application/javascript"
|
|
||||||
}
|
|
||||||
ctx.Header("Content-Type", contentType)
|
|
||||||
ctx.String(http.StatusOK, string(byteData))
|
|
||||||
ctx.Abort()
|
|
||||||
}
|
|
||||||
}, su.Handler())
|
}, su.Handler())
|
||||||
router.GET("/swagger-resources", func(ctx *gin.Context) { // lucky UI获取分组信息
|
router.GET("/swagger-resources", func(ctx *gin.Context) { // lucky UI获取分组信息
|
||||||
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域
|
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域
|
||||||
@ -151,7 +119,21 @@ func (su *SwaggerUI) Handler() func(ctx *gin.Context) {
|
|||||||
func (su *SwaggerUI) HandleLuckyUI() func(ctx *gin.Context) {
|
func (su *SwaggerUI) HandleLuckyUI() func(ctx *gin.Context) {
|
||||||
// su.router.StaticFS(su.baseUri+"/assets", http.FS(ydocUIFiles))
|
// su.router.StaticFS(su.baseUri+"/assets", http.FS(ydocUIFiles))
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
|
fileRealPath := strings.TrimPrefix(ctx.Request.RequestURI, su.baseUri)
|
||||||
|
byteData, _ := ydocUIFiles.ReadFile(define.SwaggerUIThemeYDocLucky + fileRealPath)
|
||||||
|
if strings.HasSuffix(ctx.Request.RequestURI, "html") {
|
||||||
|
byteData = []byte(strings.ReplaceAll(string(byteData), "{{BASE_URI}}", su.baseUri))
|
||||||
|
}
|
||||||
|
uriArr := strings.Split(ctx.Request.RequestURI, ".")
|
||||||
|
contentType := "text/" + uriArr[len(uriArr)-1]
|
||||||
|
if strings.HasSuffix(ctx.Request.RequestURI, "png") {
|
||||||
|
contentType = "image/png"
|
||||||
|
} else if strings.HasSuffix(ctx.Request.RequestURI, "js") {
|
||||||
|
contentType = "application/javascript"
|
||||||
|
}
|
||||||
|
ctx.Header("Content-Type", contentType)
|
||||||
|
ctx.String(http.StatusOK, string(byteData))
|
||||||
|
ctx.Abort()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user