Package swgui
(Swagger UI) provides HTTP handler to serve Swagger UI. All assets are embedded in Go source code, so
just build and run.
Static assets for v5
are built from Swagger
UI v5.17.14.
CDN-based v5cdn
uses Swagger
UI v5.17.14.
Static assets for v4
are built from Swagger
UI v4.19.1.
CDN-based v4cdn
uses Swagger
UI v4.18.3.
Static assets for v3
are built from Swagger
UI v3.52.5.
CDN-based v3cdn
uses Swagger
UI v3.52.4.
package main
import (
"net/http"
"github.com/swaggest/swgui/v5emb"
)
func main() {
http.Handle("/api1/docs/", v5emb.New(
"Petstore",
"https://petstore3.swagger.io/api/v3/openapi.json",
"/api1/docs/",
))
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("Hello World!"))
})
println("docs at http://localhost:8080/api1/docs/")
_ = http.ListenAndServe("localhost:8080", http.DefaultServeMux)
}
If you use go1.16
or later, you can import natively embedded assets with "github.com/swaggest/swgui/v5emb"
, it may
help to lower application memory usage.
In order to reduce binary size you can import github.com/swaggest/swgui/v3cdn
to use CDN hosted assets.
Also you can use swguicdn
build tag to enable CDN mode for github.com/swaggest/swgui/v3
import.
Be aware that CDN mode may be considered inappropriate for security or networking reasons.
Install swgui
.
go install github.com/swaggest/swgui/cmd/swgui@latest
Or download binary from releases.
wget https://github.com/swaggest/swgui/releases/latest/download/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz
./swgui -version
wget https://github.com/swaggest/swgui/releases/latest/download/darwin_amd64.tar.gz && tar xf darwin_amd64.tar.gz && rm darwin_amd64.tar.gz
codesign -s - ./swgui
./swgui -version
wget https://github.com/swaggest/swgui/releases/latest/download/darwin_arm64.tar.gz && tar xf darwin_arm64.tar.gz && rm darwin_arm64.tar.gz
codesign -s - ./swgui
./swgui -version
Open spec file.
swgui my-openapi.yaml