This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
138 lines (125 loc) · 3.42 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package openapi3
import (
"bytes"
"text/template"
"github.com/getkin/kin-openapi/openapi3"
"goyave.dev/goyave/v4"
"goyave.dev/goyave/v4/config"
)
// UIOptions options for the SwaggerUI Handler.
type UIOptions struct {
// Title the title of the SwaggerUI HTML document
Title string
// Favicon32 URL to a 32x32 PNG favicon
Favicon32 string
// Favicon32 URL to a 16x16 PNG favicon
Favicon16 string
// BundleURL URL to the SwaggerUI js bundle
BundleURL string
// BundleURL URL to the SwaggerUI standalone preset js bundle
PresetURL string
// StylesURL URL to the SwaggerUI CSS
StylesURL string
// Spec JSON object
Spec string
}
const (
uiTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="{{ .StylesURL }}" >
<link rel="icon" type="image/png" href="{{ .Favicon32 }}" sizes="32x32" />
<link rel="icon" type="image/png" href="{{ .Favicon16 }}" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body
{
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="{{ .BundleURL }}"> </script>
<script src="{{ .PresetURL }}"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
spec: {{ .Spec }},
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
// End Swagger UI call region
window.ui = ui
}
</script>
</body>
</html>
`
)
// NewUIOptions create a new UIOption struct with default values.
//
// By default, favicons, swagger-ui js and css use official latest available
// versions from the unpkg.com CDN.
//
// The given spec can be `nil`, in which case, you'll have to set the returned
// struct's `Spec` field to a valid JSON string.
func NewUIOptions(spec *openapi3.T) *UIOptions {
var json []byte
if spec == nil {
json = []byte{}
} else {
json, _ = spec.MarshalJSON()
}
return &UIOptions{
Title: config.GetString("app.name") + " API Documentation",
Favicon16: "https://unpkg.com/swagger-ui-dist/favicon-16x16.png",
Favicon32: "https://unpkg.com/swagger-ui-dist/favicon-32x32.png",
BundleURL: "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js",
PresetURL: "https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js",
StylesURL: "https://unpkg.com/swagger-ui-dist/swagger-ui.css",
Spec: string(json),
}
}
// Serve register the SwaggerUI route on the given router, with the given uri, and using
// the given UIOptions.
func Serve(router *goyave.Router, uri string, opts *UIOptions) {
r := router.Subrouter(uri)
tmpl := template.Must(template.New("swaggerui").Parse(uiTemplate))
buf := bytes.NewBuffer(nil)
err := tmpl.Execute(buf, opts)
if err != nil {
panic(err)
}
b := buf.Bytes()
r.Get("/", func(resp *goyave.Response, req *goyave.Request) {
resp.Header().Set("Content-Type", "text/html; charset=utf-8")
if _, err := resp.Write(b); err != nil {
panic(err)
}
})
}