podman
168 строк · 4.1 Кб
1package middleware2
3import (4"bytes"5"fmt"6"html/template"7"net/http"8"path"9)
10
11// SwaggerUIOpts configures the Swaggerui middlewares
12type SwaggerUIOpts struct {13// BasePath for the UI path, defaults to: /14BasePath string15// Path combines with BasePath for the full UI path, defaults to: docs16Path string17// SpecURL the url to find the spec for18SpecURL string19// OAuthCallbackURL the url called after OAuth2 login20OAuthCallbackURL string21
22// The three components needed to embed swagger-ui23SwaggerURL string24SwaggerPresetURL string25SwaggerStylesURL string26
27Favicon32 string28Favicon16 string29
30// Title for the documentation site, default to: API documentation31Title string32}
33
34// EnsureDefaults in case some options are missing
35func (r *SwaggerUIOpts) EnsureDefaults() {36if r.BasePath == "" {37r.BasePath = "/"38}39if r.Path == "" {40r.Path = "docs"41}42if r.SpecURL == "" {43r.SpecURL = "/swagger.json"44}45if r.OAuthCallbackURL == "" {46r.OAuthCallbackURL = path.Join(r.BasePath, r.Path, "oauth2-callback")47}48if r.SwaggerURL == "" {49r.SwaggerURL = swaggerLatest50}51if r.SwaggerPresetURL == "" {52r.SwaggerPresetURL = swaggerPresetLatest53}54if r.SwaggerStylesURL == "" {55r.SwaggerStylesURL = swaggerStylesLatest56}57if r.Favicon16 == "" {58r.Favicon16 = swaggerFavicon16Latest59}60if r.Favicon32 == "" {61r.Favicon32 = swaggerFavicon32Latest62}63if r.Title == "" {64r.Title = "API documentation"65}66}
67
68// SwaggerUI creates a middleware to serve a documentation site for a swagger spec.
69// This allows for altering the spec before starting the http listener.
70func SwaggerUI(opts SwaggerUIOpts, next http.Handler) http.Handler {71opts.EnsureDefaults()72
73pth := path.Join(opts.BasePath, opts.Path)74tmpl := template.Must(template.New("swaggerui").Parse(swaggeruiTemplate))75
76buf := bytes.NewBuffer(nil)77_ = tmpl.Execute(buf, &opts)78b := buf.Bytes()79
80return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {81if path.Join(r.URL.Path) == pth {82rw.Header().Set("Content-Type", "text/html; charset=utf-8")83rw.WriteHeader(http.StatusOK)84
85_, _ = rw.Write(b)86return87}88
89if next == nil {90rw.Header().Set("Content-Type", "text/plain")91rw.WriteHeader(http.StatusNotFound)92_, _ = rw.Write([]byte(fmt.Sprintf("%q not found", pth)))93return94}95next.ServeHTTP(rw, r)96})97}
98
99const (100swaggerLatest = "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"101swaggerPresetLatest = "https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js"102swaggerStylesLatest = "https://unpkg.com/swagger-ui-dist/swagger-ui.css"103swaggerFavicon32Latest = "https://unpkg.com/swagger-ui-dist/favicon-32x32.png"104swaggerFavicon16Latest = "https://unpkg.com/swagger-ui-dist/favicon-16x16.png"105swaggeruiTemplate = `106<!DOCTYPE html>
107<html lang="en">
108<head>
109<meta charset="UTF-8">
110<title>{{ .Title }}</title>
111
112<link rel="stylesheet" type="text/css" href="{{ .SwaggerStylesURL }}" >
113<link rel="icon" type="image/png" href="{{ .Favicon32 }}" sizes="32x32" />
114<link rel="icon" type="image/png" href="{{ .Favicon16 }}" sizes="16x16" />
115<style>
116html
117{
118box-sizing: border-box;
119overflow: -moz-scrollbars-vertical;
120overflow-y: scroll;
121}
122
123*,
124*:before,
125*:after
126{
127box-sizing: inherit;
128}
129
130body
131{
132margin:0;
133background: #fafafa;
134}
135</style>
136</head>
137
138<body>
139<div id="swagger-ui"></div>
140
141<script src="{{ .SwaggerURL }}"> </script>
142<script src="{{ .SwaggerPresetURL }}"> </script>
143<script>
144window.onload = function() {
145// Begin Swagger UI call region
146const ui = SwaggerUIBundle({
147url: '{{ .SpecURL }}',
148dom_id: '#swagger-ui',
149deepLinking: true,
150presets: [
151SwaggerUIBundle.presets.apis,
152SwaggerUIStandalonePreset
153],
154plugins: [
155SwaggerUIBundle.plugins.DownloadUrl
156],
157layout: "StandaloneLayout",
158oauth2RedirectUrl: '{{ .OAuthCallbackURL }}'
159})
160// End Swagger UI call region
161
162window.ui = ui
163}
164</script>
165</body>
166</html>
167`
168)
169