rapidoc

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

rapidoc-go

Serves a RapiDoc API reference from a Go web app: an interactive page for your OpenAPI document, with a console to try requests.

go get github.com/rapi-doc/rapidoc-go
package main

import (
	"net/http"

	rapidoc "github.com/rapi-doc/rapidoc-go"
)

func main() {
	mux := http.NewServeMux()

	docs := rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"})
	mux.Handle("/docs", docs)
	mux.Handle("/docs/", docs)

	http.ListenAndServe(":8080", mux)
}

Open /docs. Nothing is loaded from another site: the package carries the RapiDoc bundle, pinned to one release.

It is a plain net/http handler with no dependencies, so it works wherever one does. It does not generate the OpenAPI document; point it at the one your app already has, from Huma, swaggo, ogen, oapi-codegen or a file you wrote.

Options

rapidoc.Handler(rapidoc.Options{
	UIPath:     "/reference",
	SpecURL:    "/openapi.json",
	Title:      "Pets API",
	Attributes: map[string]any{"render-style": "focused", "allow-try": false},
})
Option Default
UIPath /docs URL path the reference page is served at: the path you mount the handler on. / is the site root
SpecURL URL of your OpenAPI document, which the browser fetches when the page opens. Use it when the app already serves the document
Spec The OpenAPI document itself: JSON as []byte, json.RawMessage or string, or a value that encoding/json marshals to it. It is written into the page, so the browser makes no request for it. With both, Spec is shown and SpecURL is the download link. YAML is not read here; serve it and use SpecURL
Title API Reference Page title
Assets rapidoc.AssetsBundled AssetsBundled: the app serves rapidoc-min.js. rapidoc.AssetsCDN: the browser loads the same version from jsDelivr, checked against its SRI hash. rapidoc.AssetsURL(url, integrity): a URL of your own
Attributes Attributes of <rapi-doc>, in kebab-case or camelCase. They win over the package's own, and nil removes one
Head HTML added to the end of <head>, as given
CSS CSS added in a <style> element, as given
Lang en Language of the page
Nonce func(*http.Request) string that reads the request's Content-Security-Policy nonce, which is then set on every <script> and <style>
PublicUIPath UIPath UIPath as the browser sees it, when http.StripPrefix or a reverse proxy removes a prefix before the request reaches the handler

These are the options of every RapiDoc integration, in Go naming.

Handler panics on options that cannot be used (an attribute name that is not valid, a Spec that is not JSON); NewHandler returns the error instead. Middleware(options) serves the reference and passes every other request to the next handler.

With a router or framework

The handler matches the full request path, so mount it without stripping the prefix.

// chi
r.Mount("/docs", rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))

// Gin
docs := gin.WrapH(rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))
router.GET("/docs", docs)
router.GET("/docs/*file", docs)

// Echo
docs := echo.WrapHandler(rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))
e.GET("/docs", docs)
e.GET("/docs/*", docs)

// Fiber (github.com/gofiber/fiber/v2/middleware/adaptor)
app.Use("/docs", adaptor.HTTPHandler(rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"})))
Huma

Huma generates the OpenAPI document and serves it at /openapi.json. Turn off its built-in docs page and mount RapiDoc on the router underneath:

router := chi.NewMux()

config := huma.DefaultConfig("Pets API", "1.0.0")
config.DocsPath = "" // RapiDoc serves /docs
api := humachi.New(router, config)

router.Mount("/docs", rapidoc.Handler(rapidoc.Options{
	SpecURL: "/openapi.json",
	Title:   "Pets API",
}))

Notes

  • The handler answers GET and HEAD on the page, {UIPath}/oauth-receiver.html, {UIPath}/rapidoc-min.js and its license file. Any other path is a 404 (or goes to the next handler, with Middleware).
  • OAuth sign-in returns to {UIPath}/oauth-receiver.html, served on your own origin, also with AssetsCDN.
  • The bundle is sent with an ETag, and rapidoc-min.js.LICENSE.txt next to it lists the libraries inside it.
  • rapidoc.Version() is the RapiDoc release the package carries (the file RAPIDOC_VERSION).
  • The page, its routes and its HTML follow the RapiDoc integration contract, which the integrations for JavaScript, Python and .NET also implement; testdata/conformance holds its fixtures.

Go 1.22 or later. MIT.

Releasing (for maintainers)

A release is a git tag; the Go module proxy picks it up, and there is no registry account or upload.

  1. Choose the bundle. To ship a newer RapiDoc, run scripts/sync.sh <version> (for example scripts/sync.sh 12.3.0). It replaces assets/, the fixtures in testdata/conformance/ and RAPIDOC_VERSION. Skip this for a release that only changes the Go code.

  2. Check. go test ./...

  3. Merge to main and wait for the Test workflow.

  4. Tag and push:

    git tag v0.1.1
    git push origin v0.1.1
    
  5. Verify. go get github.com/rapi-doc/rapidoc-go@v0.1.1 in a new module, and check https://pkg.go.dev/github.com/rapi-doc/rapidoc-go (the first request for a new version makes the proxy fetch it).

Which number to raise: a fix or a newer bundle is a patch (v0.1.1); a new option is a minor (v0.2.0). While the version is v0.x, a change that breaks existing code is also a minor, and is called out in the release notes. A tag cannot be moved or reused once the proxy has seen it, so fix a bad release with a new tag.

Documentation

Overview

Package rapidoc serves a RapiDoc API reference from a Go web app: an interactive page for an OpenAPI document, with a console to try requests.

mux := http.NewServeMux()
mux.Handle("/docs/", rapidoc.Handler(rapidoc.Options{SpecURL: "/openapi.json"}))

The handler is a plain net/http handler with no dependencies, so it works with the standard mux, chi and gorilla directly, and with Gin, Echo and Fiber through their net/http adapters. The RapiDoc bundle is inside the package (go:embed); nothing is loaded from another site unless Assets says so.

The page, its routes and its HTML follow the RapiDoc integration contract, the same one the integrations for other languages implement: https://rapidocui.com/docs/get-started/build-an-integration/

Index

Constants

View Source
const ContractVersion = 2

ContractVersion is the version of the integration contract this package implements.

Variables

View Source
var (
	// AssetsBundled has the app serve rapidoc-min.js, which is inside this package. The default.
	AssetsBundled = Assets{}
	// AssetsCDN has the browser load the same version from jsDelivr, checked against its SRI hash.
	AssetsCDN = Assets{/* contains filtered or unexported fields */}
)

Functions

func Handler

func Handler(opts Options) http.Handler

Handler is NewHandler for options known to be valid: it panics where NewHandler returns an error.

func Middleware

func Middleware(opts Options) func(http.Handler) http.Handler

Middleware serves the reference like Handler and passes every other request to the next handler, for routers that take middleware (chi's Use) and for a reference at the site root. It panics on options that cannot be used.

func NewHandler

func NewHandler(opts Options) (http.Handler, error)

NewHandler returns a handler that serves the reference at opts.UIPath, with the OAuth return page and, unless the bundle comes from a CDN, rapidoc-min.js under it. It answers GET and HEAD; any other path is a 404. The error reports options that cannot be used, such as an attribute name that is not valid.

func Version

func Version() string

Version is the version of rapidoc (the npm package) inside this package.

Types

type Assets

type Assets struct {
	// contains filtered or unexported fields
}

Assets says where the browser loads the RapiDoc bundle from. The zero value is AssetsBundled.

func AssetsURL

func AssetsURL(scriptURL, integrity string) Assets

AssetsURL is a script URL of your own, with an optional SRI hash.

type Options

type Options struct {
	// UIPath is the URL path the reference page is served at: the path the handler is mounted on.
	// Default "/docs"; "/" is the site root.
	UIPath string

	// PublicUIPath is UIPath as the browser sees it. Set it only when a prefix is removed before the
	// request reaches the handler, by http.StripPrefix or a reverse proxy: with the handler at "/docs"
	// behind a proxy that strips "/api", it is "/api/docs".
	PublicUIPath string

	// SpecURL is the URL of the OpenAPI document, which the browser fetches when the page opens.
	SpecURL string

	// Spec is the OpenAPI document itself: JSON as []byte, json.RawMessage or string, or any value that
	// encoding/json marshals to it. It is written into the page, so the browser makes no request for it.
	// With both, Spec is shown and SpecURL is the download link. (YAML is not read; use SpecURL for it.)
	Spec any

	// Title is the page title. Default "API Reference".
	Title string

	// Assets says where the browser loads the bundle from. Default AssetsBundled.
	Assets Assets

	// Attributes are attributes of <rapi-doc> by name, in kebab-case or camelCase:
	// {"render-style": "focused", "allowTry": false}. They win over the package's own, and a nil value
	// removes one. See https://rapidocui.com/api/.
	Attributes map[string]any

	// Head is HTML added to the end of <head>, as given.
	Head string

	// CSS is added in a <style> element, as given.
	CSS string

	// Lang is the language of the page. Default "en".
	Lang string

	// Nonce reads the request's Content-Security-Policy nonce, which is set on every <script> and <style>.
	Nonce func(*http.Request) string
}

Options are the options of every RapiDoc integration, in Go naming.

Directories

Path Synopsis
examples
basic command
go run ./examples/basic → http://localhost:4408/docs
go run ./examples/basic → http://localhost:4408/docs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL