package middleware import ( "bytes" "net/http" "strconv" "strings" swaggerFiles "github.com/swaggo/files" "github.com/gin-gonic/gin" ) func SwaggerHandler() gin.HandlerFunc { return func(c *gin.Context) { rawPath := c.Request.URL.Path relPath := strings.TrimPrefix(rawPath, "/swagger") if relPath == "" || relPath == "/" { c.Request.URL.Path = "/index.html" } else { 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) c.Request.URL.Path = rawPath } } type captureResponseWriter struct { ginWriter gin.ResponseWriter code int buf bytes.Buffer } 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) WriteHeader(code int) { w.code = code } const brandingCSS = ` /* MengStack API branding — replace Swagger SmartBear logo */ .topbar { display: flex !important; align-items: center !important; background-color: #1b1b1f !important; padding: 8px 20px !important; min-height: 50px !important; position: relative !important; } .topbar > * { visibility: hidden !important; width: 0 !important; height: 0 !important; overflow: hidden !important; margin: 0 !important; padding: 0 !important; flex: 0 !important; } .topbar::before { content: "MengStack API \6587\6863" !important; visibility: visible !important; width: auto !important; height: auto !important; overflow: visible !important; flex: 0 0 auto !important; color: #fff !important; font-size: 20px !important; font-weight: 700 !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important; letter-spacing: 1px !important; text-decoration: none !important; position: absolute !important; left: 20px !important; top: 50% !important; transform: translateY(-50%) !important; } .topbar::after { content: "v0.3.0" !important; visibility: visible !important; width: auto !important; height: auto !important; overflow: visible !important; flex: 0 0 auto !important; color: rgba(255,255,255,0.5) !important; font-size: 12px !important; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important; position: absolute !important; left: 230px !important; top: 50% !important; transform: translateY(-50%) !important; } .swagger-ui { margin-top: 0 !important; } `