http-swagger: Getting 404 for CSS and JS files
I am new to go lang. My go lang version: 1.11.3 I am using Gorilla mux for router.
Somehow I cannot able to integrate swaggo/swag in to my Go Web application. Please see the steps I have taken.
Step 1: Added comments to my API (main.go) source code (Refer: https://github.com/swaggo/http-swagger#canonical-example)
Step 2: Download swag and http-swagger
go get github.com/swaggo/swag/cmd/swag
go get -u github.com/swaggo/http-swagger
Step 3: Run swag (it will generate some files in a docs/ directory)
swag init
Add the following code in my main.go
import (
"github.com/swaggo/http-swagger"
_"mylocation/docs"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.PathPrefix("/documentation/").Handler(httpSwagger.WrapHandler)
http.ListenAndServe(":8000", router)
}
I can able to redirect to “/documentation” route but page is blank. And when I check network logs, following files are missing/ getting 404
- swagger-ui.css
- swagger-ui-bundle.js
- swagger-ui-standalone-preset.js
I don’t know what exactly happening. Please help
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 6
- Comments: 15 (2 by maintainers)
Hi,
I am getting the same issue but using echo-swagger
After more than 48 hours working, suddenly the app starts to drop 404 loading css, js that you mentioned.
If i restart app, it starts to work… but i dont know why it happens.
Hey everyone! I had the same issue but looks like the version 2.0.1 fixes it removing the singleton (https://github.com/swaggo/http-swagger/commit/dce10a857ace56c9b2ecb27333988e3c4a559787#diff-44aa764656674984f26bf27a7bcd56f8c03ea4f6b1ee8b02c48bd7e6c6c8206fL172)
I upgraded and now it’s under test.
@umurpza this is the solution I came up with, and it has been working fine. It’s not ideal because I have to make a new handler internally for each request because of how
httpSwagger.Handlerworks, but it at least got me going.You can do it like this:
Do not forget import doc files: