fix: integrated CSS branding into SwaggerHandler
Some checks failed
CI / Build & Test (push) Has been cancelled
Some checks failed
CI / Build & Test (push) Has been cancelled
This commit is contained in:
parent
267ec57985
commit
493947c987
@ -22,50 +22,46 @@ func SwaggerHandler() gin.HandlerFunc {
|
|||||||
c.Request.URL.Path = relPath
|
c.Request.URL.Path = relPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isCSS := strings.HasSuffix(relPath, "swagger-ui.css")
|
||||||
|
|
||||||
|
if isCSS {
|
||||||
|
crw := &captureResponseWriter{
|
||||||
|
ginWriter: c.Writer,
|
||||||
|
code: 200,
|
||||||
|
}
|
||||||
|
c.Writer = crw
|
||||||
|
|
||||||
|
swaggerFiles.Handler.ServeHTTP(crw, c.Request)
|
||||||
|
|
||||||
|
c.Writer = crw.ginWriter
|
||||||
|
|
||||||
|
if crw.code == 200 && crw.buf.Len() > 0 {
|
||||||
|
body := append(crw.buf.Bytes(), []byte(brandingCSS)...)
|
||||||
|
c.Header("Content-Length", strconv.Itoa(len(body)))
|
||||||
|
c.Header("Content-Type", "text/css; charset=utf-8")
|
||||||
|
c.Writer.WriteHeader(crw.code)
|
||||||
|
c.Writer.Write(body)
|
||||||
|
c.Request.URL.Path = rawPath
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if crw.buf.Len() > 0 {
|
||||||
|
c.Writer.WriteHeader(crw.code)
|
||||||
|
c.Writer.Write(crw.buf.Bytes())
|
||||||
|
c.Request.URL.Path = rawPath
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Writer.WriteHeader(crw.code)
|
||||||
|
c.Request.URL.Path = rawPath
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
swaggerFiles.Handler.ServeHTTP(c.Writer, c.Request)
|
swaggerFiles.Handler.ServeHTTP(c.Writer, c.Request)
|
||||||
|
|
||||||
c.Request.URL.Path = rawPath
|
c.Request.URL.Path = rawPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SwaggerCSSBranding() gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
if !strings.HasSuffix(c.Request.URL.Path, "swagger-ui.css") {
|
|
||||||
c.Next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
underlying := c.Writer
|
|
||||||
crw := &captureResponseWriter{
|
|
||||||
ginWriter: underlying,
|
|
||||||
code: 200,
|
|
||||||
}
|
|
||||||
c.Writer = crw
|
|
||||||
|
|
||||||
c.Next()
|
|
||||||
|
|
||||||
c.Writer = underlying
|
|
||||||
|
|
||||||
if crw.code == 200 && crw.buf.Len() > 0 {
|
|
||||||
body := append(crw.buf.Bytes(), []byte(brandingCSS)...)
|
|
||||||
h := underlying.Header()
|
|
||||||
h.Set("Content-Length", strconv.Itoa(len(body)))
|
|
||||||
h.Set("Content-Type", "text/css; charset=utf-8")
|
|
||||||
underlying.WriteHeader(crw.code)
|
|
||||||
underlying.Write(body)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if crw.buf.Len() > 0 {
|
|
||||||
underlying.WriteHeader(crw.code)
|
|
||||||
underlying.Write(crw.buf.Bytes())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
underlying.WriteHeader(crw.code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type captureResponseWriter struct {
|
type captureResponseWriter struct {
|
||||||
ginWriter gin.ResponseWriter
|
ginWriter gin.ResponseWriter
|
||||||
code int
|
code int
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user