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 ¶
const ContractVersion = 2
ContractVersion is the version of the integration contract this package implements.
Variables ¶
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 ¶
Handler is NewHandler for options known to be valid: it panics where NewHandler returns an error.
func Middleware ¶
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 ¶
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.
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.
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.