gin-swagger
gin-swagger copied to clipboard
How to configure swagger UI so it doesn't need the URL?
I am using http://github.com/swaggo/gin-swagger to auto-generate the swagger and also create the UI as below.
Everything works fine when I am running the server on my machine but when I run the server on Kubernetes the UI cannot find the doc.json and shows Failed to load spec.
and in the browser I get Failed to load resource: net::ERR_CONNECTION_REFUSED localhost:9080/swagger/doc.json:1
.
As it can be seen the problem is the URL that points to the localhost and it is not accessible from outside of the cluster. If I use http://my_public_url/reverse_proxy_entry_for_my_service/swagger/doc.json
then everything is working fine and in the UI I can see the generated swagger.
My question is how I can remove this dependency to the public_url? I need to launch my component on multiple URL and if possible I want to make it independent from the URL otherwise I have to use an environment variable to pass and configure the URL.
My code:
url := ginSwagger.URL("http://localhost:9080/swagger/doc.json")
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
I also trie /swagger/doc.json
which returned 404 and Failed to load spec
and also swagger/doc.json
which returned bad gateway from Nginx in front.
Please see #89. Here is an example:
func main() {
r := gin.New()
r.GET("/swagger/*any", func(context *gin.Context) {
docs.SwaggerInfo.Host = context.Request.Host
ginSwagger.CustomWrapHandler(&ginSwagger.Config{URL: "/swagger/doc.json"}, swaggerFiles.Handler)(context)
})
r.Run(":8080")
}
The host parameter should be left alone and everything should be fine. If you intend to set the Host use an FQDN value instead of an IP address.
@avarf Is there anything else we can help you with