swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.76k stars 270 forks source link

Getting 404 for CSS and JS files #225

Open applejag opened 2 years ago

applejag commented 2 years ago

The CSS & JS files all respond with 404. Even using the example from the README.md I still get the same results.

https://github.com/swaggo/gin-swagger/blob/b9e926c528e8b8269d0241b3053144235eef4e90/README.md?plain=1#L99-L136

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /api/v1/example/helloworld --> main.Helloworld (3 handlers)
[GIN-debug] GET    /swagger/*any             --> github.com/swaggo/gin-swagger.CustomWrapHandler.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2022/08/04 - 12:43:51 | 200 |     149.986µs |       127.0.0.1 | GET      "/swagger/index.html"
[GIN] 2022/08/04 - 12:43:51 | 404 |      14.294µs |       127.0.0.1 | GET      "/swagger/swagger-ui-bundle.js"
[GIN] 2022/08/04 - 12:43:51 | 404 |      20.702µs |       127.0.0.1 | GET      "/swagger/swagger-ui-standalone-preset.js"
[GIN] 2022/08/04 - 12:43:51 | 404 |       6.681µs |       127.0.0.1 | GET      "/swagger/swagger-ui.css"

Maybe related: https://github.com/swaggo/http-swagger/issues/10, don't know if the issue is in this codebase or in some dependency, but it's not working here as well.

bebetterLWJ commented 1 year ago

you can get in ginswagger.WrapHandler -> CustomWrapHandler you can see once.Do(func() { handler.Prefix = matches[1] }) and handler.ServeHTTP(c.Writer, c.Request), that means when the first request url is where .js、.css in, so you have to use the same url as the first request url or replace /swagger/*any with a specific route