From https://medium.com/@ribice/serve-swaggerui-within-your-golang-application-5486748a5ed4
SwaggerUI can be downloaded from their GitHub Repo Releases page. Once downloaded, place the contents of dist
folder somewhere in your Go project. For example, swaggerui.
After that, also move your openapi.json or swagger.json file to swaggerui
folder (or whatever you called it), and inside index.html
change url to ./swagger.json
(e.g. url: "./swagger.json"
).
fs := http.FileServer(http.Dir("./swaggerui"))
http.Handle("/swaggerui/", http.StripPrefix("/swaggerui/", fs))
sh := http.StripPrefix("/swaggerui/", http.FileServer(http.Dir("./swaggerui/")))
r.PathPrefix("/swaggerui/").Handler(sh)
r.Static("/swaggerui", "cmd/api/swaggerui")
r.Static("/swaggerui/", "cmd/api/swaggerui")
sh := http.StripPrefix("/swaggerui/", http.FileServer(http.Dir("./swaggerui/")))
r.Get("/swaggerui/", sh)