Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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