gin-swagger icon indicating copy to clipboard operation
gin-swagger copied to clipboard

How to configure swagger UI so it doesn't need the URL?

Open avarf opened this issue 3 years ago • 3 comments

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.

avarf avatar Mar 10 '21 13:03 avarf

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")
}

skunkie avatar Jun 16 '21 14:06 skunkie

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.

ubogdan avatar Oct 04 '21 20:10 ubogdan

@avarf Is there anything else we can help you with

ubogdan avatar Nov 09 '21 07:11 ubogdan