Skip to main content

Swagger UI

Swagger UI is the most widely used OpenAPI documentation interface. It provides an interactive API explorer familiar to most developers.

Installation

go get github.com/oaswrap/spec-ui/swaggerui

Usage

import (
specui "github.com/oaswrap/spec-ui"
"github.com/oaswrap/spec-ui/swaggerui"
)

handler := specui.NewHandler(
specui.WithTitle("My API"),
specui.WithSpecFile("openapi.yaml"),
swaggerui.WithUI(),
)

With Configuration

import (
"github.com/oaswrap/spec-ui/config"
"github.com/oaswrap/spec-ui/swaggerui"
)

handler := specui.NewHandler(
specui.WithTitle("My API"),
specui.WithSpecFile("openapi.yaml"),
swaggerui.WithUI(config.SwaggerUI{
HideCurl: false,
JsonEditor: false,
Layout: "StandaloneLayout",
DefaultModelsExpandDepth: 1,
}),
)

Configuration Reference

FieldTypeDefaultDescription
HideCurlboolfalseHide curl code snippets
JsonEditorboolfalseEnable visual JSON editor (experimental)
Layoutstring"StandaloneLayout"Layout: "StandaloneLayout" or "BaseLayout"
DefaultModelsExpandDepthint1Default depth for model expansion in the UI

Embedded Mode

Use swaggeruiemb for air-gapped deployments:

import "github.com/oaswrap/spec-ui/swaggeruiemb"

handler := specui.NewHandler(
specui.WithSpecFile("openapi.yaml"),
swaggeruiemb.WithUI(),
)

if handler.AssetsEnabled() {
r.Get(handler.AssetsPath()+"/*", handler.Assets().ServeHTTP)
}