fix: integrated CSS branding into SwaggerHandler
Some checks are pending
CI / Build & Test (push) Waiting to run
Some checks are pending
CI / Build & Test (push) Waiting to run
This commit is contained in:
parent
267ec57985
commit
7d5dc771e7
@ -1,9 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -22,50 +20,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
|
||||||
@ -75,17 +69,6 @@ type captureResponseWriter struct {
|
|||||||
func (w *captureResponseWriter) Header() http.Header { return w.ginWriter.Header() }
|
func (w *captureResponseWriter) Header() http.Header { return w.ginWriter.Header() }
|
||||||
func (w *captureResponseWriter) Write(b []byte) (int, error) { return w.buf.Write(b) }
|
func (w *captureResponseWriter) Write(b []byte) (int, error) { return w.buf.Write(b) }
|
||||||
func (w *captureResponseWriter) WriteHeader(code int) { w.code = code }
|
func (w *captureResponseWriter) WriteHeader(code int) { w.code = code }
|
||||||
func (w *captureResponseWriter) WriteString(s string) (int, error) { return w.buf.WriteString(s) }
|
|
||||||
func (w *captureResponseWriter) Status() int { return w.code }
|
|
||||||
func (w *captureResponseWriter) Size() int { return w.buf.Len() }
|
|
||||||
func (w *captureResponseWriter) Written() bool { return w.buf.Len() > 0 }
|
|
||||||
func (w *captureResponseWriter) WriteHeaderNow() {}
|
|
||||||
func (w *captureResponseWriter) Pusher() http.Pusher { return nil }
|
|
||||||
func (w *captureResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
||||||
return w.ginWriter.Hijack()
|
|
||||||
}
|
|
||||||
func (w *captureResponseWriter) CloseNotify() <-chan bool { return w.ginWriter.CloseNotify() }
|
|
||||||
func (w *captureResponseWriter) Flush() {}
|
|
||||||
|
|
||||||
const brandingCSS = `
|
const brandingCSS = `
|
||||||
/* MengStack API branding — replace Swagger SmartBear logo */
|
/* MengStack API branding — replace Swagger SmartBear logo */
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user