Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Created May 31, 2020 03:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenACoffman/fe5f7774c750926210b642a0997479b0 to your computer and use it in GitHub Desktop.
Save StevenACoffman/fe5f7774c750926210b642a0997479b0 to your computer and use it in GitHub Desktop.
golang serve Swagger-ui

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

Serve using net/http

fs := http.FileServer(http.Dir("./swaggerui"))
http.Handle("/swaggerui/", http.StripPrefix("/swaggerui/", fs))

Serve using Gorilla Mux (commit)

sh := http.StripPrefix("/swaggerui/", http.FileServer(http.Dir("./swaggerui/")))
r.PathPrefix("/swaggerui/").Handler(sh)

Serve using Echo (commit)

r.Static("/swaggerui", "cmd/api/swaggerui")

Serve using Gin (commit)

r.Static("/swaggerui/", "cmd/api/swaggerui")

Serve using Chi

sh := http.StripPrefix("/swaggerui/", http.FileServer(http.Dir("./swaggerui/")))
r.Get("/swaggerui/", sh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment