fix: return index.html for /swagger instead of redirecting (static server-like behavior)

This commit is contained in:
illionillion 2025-11-24 14:52:23 +09:00
parent 2b8554dea5
commit 48b8bcf40c
2 changed files with 11 additions and 0 deletions

View File

@ -159,6 +159,12 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc
var matcher = regexp.MustCompile(`(.*)(index\.html|index\.css|swagger-initializer\.js|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[?|.]*`) var matcher = regexp.MustCompile(`(.*)(index\.html|index\.css|swagger-initializer\.js|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[?|.]*`)
return func(ctx *gin.Context) { return func(ctx *gin.Context) {
// Return index.html content for /swagger or /swagger/
if ctx.Request.Method == http.MethodGet && (ctx.Request.RequestURI == "/swagger" || ctx.Request.RequestURI == "/swagger/") {
ctx.Header("Content-Type", "text/html; charset=utf-8")
_ = index.Execute(ctx.Writer, config.toSwaggerConfig())
return
}
if ctx.Request.Method != http.MethodGet { if ctx.Request.Method != http.MethodGet {
ctx.AbortWithStatus(http.StatusMethodNotAllowed) ctx.AbortWithStatus(http.StatusMethodNotAllowed)

View File

@ -80,6 +80,11 @@ func TestWrapCustomHandler(t *testing.T) {
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, "/index.html", router).Code) assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, "/index.html", router).Code)
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPut, "/index.html", router).Code) assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPut, "/index.html", router).Code)
// Test: /swagger should redirect to /swagger/index.html (not implemented yet, should fail)
w := performRequest(http.MethodGet, "/swagger", router)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "<title>Swagger UI</title>")
} }
func TestDisablingWrapHandler(t *testing.T) { func TestDisablingWrapHandler(t *testing.T) {