fix: serve doc.json via swag.ReadDoc in SwaggerHandler
Some checks failed
CI / Build & Test (push) Has been cancelled

The custom SwaggerHandler was missing doc.json, causing Swagger UI
to fail loading the API specification.
This commit is contained in:
MengStack Dev 2026-10-03 04:23:31 +08:00
parent ec352306f1
commit f44fcbafa7

View File

@ -9,6 +9,7 @@ import (
"strings"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/swag"
"github.com/gin-gonic/gin"
)
@ -24,6 +25,16 @@ func SwaggerHandler() gin.HandlerFunc {
isCSS := strings.HasSuffix(relPath, "swagger-ui.css")
isInitializer := strings.HasSuffix(relPath, "swagger-initializer.js")
isDocJSON := strings.HasSuffix(relPath, "doc.json")
if isDocJSON {
doc := swag.ReadDoc()
c.Header("Content-Type", "application/json; charset=utf-8")
c.Writer.WriteHeader(200)
c.Writer.Write([]byte(doc))
c.Request.URL.Path = rawPath
return
}
if isInitializer {
c.Header("Content-Type", "application/javascript; charset=utf-8")