Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve Swagger UI on custom route without path or path extensions #284

Open
bmeverett opened this issue Sep 25, 2023 · 1 comment
Open

Serve Swagger UI on custom route without path or path extensions #284

bmeverett opened this issue Sep 25, 2023 · 1 comment

Comments

@bmeverett
Copy link

I would like to be able to serve the swagger UI on a route like /docs or /swagger without having to provide the full path and extension /swagger/index.html. Is there currently a way to do this or something that could be supported?

@juampiq6
Copy link

Hey!
I was having the same issue declaring a custom route. I did manage to create a partial solution.

func setupSwagger(e *gin.Engine) {
	e.GET("/swagger", func(ctx *gin.Context) {
		ctx.Redirect(http.StatusPermanentRedirect, "/swagger/index.html")
	})
	e.GET("/docs/*any", func(ctx *gin.Context) {
		ctx.Redirect(http.StatusPermanentRedirect, "/swagger/index.html")
	})
	e.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}

I declared some routes where you can redirect to the main swagger assets handler to serve the request.
So with this hack if you request /swagger, /docs, /docs/ or even /docs/adsasdfasdf that will perfectly redirect to the main swagger handler correctly /swagger/index.html.
The only case i couldnt not solve was /swagger/ because this case is handled by the swagger wildcard route and the ginSwagger.WrapHandler cannot resolve any asset to serve from that route so it responds with 404.
Note that this is a hack and a better solution to serve these files could be implemented in this library, but maybe not that neccessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants