Skip to content

Latest commit

 

History

History
59 lines (42 loc) · 2.19 KB

README.md

File metadata and controls

59 lines (42 loc) · 2.19 KB

Swagger UI Go Bindata

go-doc Go Report Card GitHub license

This package embeds swagger-ui-dist using go-bindata and exposes an http.Handler to integrate in existing HTTP server projects. The generated bindata is available at go.skymeyer.dev/swagger-ui-bindata/bindata and is generated with the -fs option exposing an http.FileSystem interface.

Usage

For the default Swagger UI, follow below code sample:

package main

import (
	"net/http"
	swaggerui "go.skymeyer.dev/swagger-ui-bindata"
)

func main() {
	http.ListenAndServe(":8080", swaggerui.New().Handler())
}

Options

More useful is the ability to set the OpenAPI spec you want to display instead of the default demo Petstore. This can be achieved by using the swaggerui.WithSpecURL option.

swaggerui.New().Handler(
    swaggerui.WithSpecURL("https://foobar.com/openapi.yaml"),
)

Alternatively, the OpenAPI spec can also be passed in []byte format if you wish to embed it directly in the Go binary. For this, use the swaggerui.WithEmbeddedSpec option.

swaggerui.New().Handler(
    swaggerui.WithEmbeddedSpec(bindata.MustAsset("openapi.yaml")),
)

If both WithEmbeddedSpec and swaggerui.WithSpecURL are specified, then WithEmbeddedSpec has precedence.

When not serving Swagger UI from the root, use swaggerui.WithPrefix to match the sub path accordingly:

mux := http.NewServeMux()
mux.Handle("/swagger-ui/", swaggerui.New(
	swaggerui.WithPrefix("/swagger-ui/"),
).Handler())
http.ListenAndServe(":8080", mux)

Example

Run the example code using go run ./example and point your browser to http://localhost:8080/swagger-ui.