podman
103 строки · 2.4 Кб
1package middleware
2
3import (
4"bytes"
5"fmt"
6"html/template"
7"net/http"
8"path"
9)
10
11// RedocOpts configures the Redoc middlewares
12type RedocOpts 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// RedocURL for the js that generates the redoc site, defaults to: https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js
20RedocURL 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 *RedocOpts) EnsureDefaults() {
27if r.BasePath == "" {
28r.BasePath = "/"
29}
30if r.Path == "" {
31r.Path = "docs"
32}
33if r.SpecURL == "" {
34r.SpecURL = "/swagger.json"
35}
36if r.RedocURL == "" {
37r.RedocURL = redocLatest
38}
39if r.Title == "" {
40r.Title = "API documentation"
41}
42}
43
44// Redoc 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 Redoc(opts RedocOpts, next http.Handler) http.Handler {
48opts.EnsureDefaults()
49
50pth := path.Join(opts.BasePath, opts.Path)
51tmpl := template.Must(template.New("redoc").Parse(redocTemplate))
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 (
77redocLatest = "https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"
78redocTemplate = `<!DOCTYPE html>
79<html>
80<head>
81<title>{{ .Title }}</title>
82<!-- needed for adaptive design -->
83<meta charset="utf-8"/>
84<meta name="viewport" content="width=device-width, initial-scale=1">
85<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
86
87<!--
88ReDoc doesn't change outer page styles
89-->
90<style>
91body {
92margin: 0;
93padding: 0;
94}
95</style>
96</head>
97<body>
98<redoc spec-url='{{ .SpecURL }}'></redoc>
99<script src="{{ .RedocURL }}"> </script>
100</body>
101</html>
102`
103)
104