podman
90 строк · 2.2 Кб
1package middleware
2
3import (
4"bytes"
5"fmt"
6"html/template"
7"net/http"
8"path"
9)
10
11// RapiDocOpts configures the RapiDoc middlewares
12type RapiDocOpts struct {
13// BasePath for the UI path, defaults to: /
14BasePath string
15// Path combines with BasePath for the full UI path, defaults to: docs
16Path string
17// SpecURL the url to find the spec for
18SpecURL string
19// RapiDocURL for the js that generates the rapidoc site, defaults to: https://cdn.jsdelivr.net/npm/rapidoc/bundles/rapidoc.standalone.js
20RapiDocURL string
21// Title for the documentation site, default to: API documentation
22Title string
23}
24
25// EnsureDefaults in case some options are missing
26func (r *RapiDocOpts) EnsureDefaults() {
27if r.BasePath == "" {
28r.BasePath = "/"
29}
30if r.Path == "" {
31r.Path = "docs"
32}
33if r.SpecURL == "" {
34r.SpecURL = "/swagger.json"
35}
36if r.RapiDocURL == "" {
37r.RapiDocURL = rapidocLatest
38}
39if r.Title == "" {
40r.Title = "API documentation"
41}
42}
43
44// RapiDoc creates a middleware to serve a documentation site for a swagger spec.
45// This allows for altering the spec before starting the http listener.
46//
47func RapiDoc(opts RapiDocOpts, next http.Handler) http.Handler {
48opts.EnsureDefaults()
49
50pth := path.Join(opts.BasePath, opts.Path)
51tmpl := template.Must(template.New("rapidoc").Parse(rapidocTemplate))
52
53buf := bytes.NewBuffer(nil)
54_ = tmpl.Execute(buf, opts)
55b := buf.Bytes()
56
57return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
58if r.URL.Path == pth {
59rw.Header().Set("Content-Type", "text/html; charset=utf-8")
60rw.WriteHeader(http.StatusOK)
61
62_, _ = rw.Write(b)
63return
64}
65
66if next == nil {
67rw.Header().Set("Content-Type", "text/plain")
68rw.WriteHeader(http.StatusNotFound)
69_, _ = rw.Write([]byte(fmt.Sprintf("%q not found", pth)))
70return
71}
72next.ServeHTTP(rw, r)
73})
74}
75
76const (
77rapidocLatest = "https://unpkg.com/rapidoc/dist/rapidoc-min.js"
78rapidocTemplate = `<!doctype html>
79<html>
80<head>
81<title>{{ .Title }}</title>
82<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 charecters -->
83<script type="module" src="{{ .RapiDocURL }}"></script>
84</head>
85<body>
86<rapi-doc spec-url="{{ .SpecURL }}"></rapi-doc>
87</body>
88</html>
89`
90)
91