-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.go
166 lines (150 loc) · 4.33 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package soda
import (
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
type UIRender interface {
Render(doc *openapi3.T) string
}
var (
UISwaggerUI = builtinUIRender{template: uiSwaggerUI}
UIRapiDoc = builtinUIRender{template: uiRapiDoc}
UIStoplightElement = builtinUIRender{template: uiStoplightElement}
UIRedoc = builtinUIRender{template: uiRedoc}
)
type builtinUIRender struct {
template string
cached string
}
func (u builtinUIRender) Render(doc *openapi3.T) string {
if u.cached == "" {
spec, _ := doc.MarshalJSON()
replacer := strings.NewReplacer(
"{:title}", doc.Info.Title,
"{:spec}", string(spec),
)
u.cached = replacer.Replace(u.template)
}
return u.cached
}
const uiSwaggerUI = `
<!DOCTYPE html>
<html charset="UTF-8">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>{:title} Document [Swagger UI]</title>
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css">
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
</head>
</html>
<body>
<div id="ui"></div>
<script>
let spec = {:spec};
let oauth2RedirectUrl;
let query = window.location.href.indexOf("?");
if (query > 0) {
oauth2RedirectUrl = window.location.href.substring(0, query);
} else {
oauth2RedirectUrl = window.location.href;
}
if (!oauth2RedirectUrl.endsWith("/")) {
oauth2RedirectUrl += "/";
}
oauth2RedirectUrl += "oauth-receiver.html";
SwaggerUIBundle({
dom_id: '#ui',
spec: spec,
filter: false,
oauth2RedirectUrl: oauth2RedirectUrl,
})
</script>`
const uiRapiDoc = `
<!DOCTYPE html>
<html charset="UTF-8">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>{:title} Document [RapiDoc]</title>
<script type="module" src="https://cdn.jsdelivr.net/npm/rapidoc/dist/rapidoc-min.min.js"></script>
</head>
<style>
rapi-doc::part(section-navbar) { /* <<< targets navigation bar */
background: linear-gradient(90deg, #3d4e70, #2e3746);
}
</style>
<body>
<rapi-doc id="thedoc"
theme="dark"
primary-color = "#f54c47"
bg-color = "#2e3746"
text-color = "#bacdee"
default-schema-tab="model"
allow-search="false"
allow-advanced-search="true"
show-info="true"
show-header="true"
show-components="true"
schema-style="table"
show-method-in-nav-bar="as-colored-block"
allow-try="true"
allow-authentication="true"
regular-font="Open Sans"
mono-font="Roboto Mono"
font-size="large"
schema-description-expanded="true">
</rapi-doc>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
let docEl = document.getElementById("thedoc");
docEl.loadSpec({:spec});
})
</script>
</body>
</html>`
const uiStoplightElement = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{:title} Document [Elements]</title>
<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">
</head>
<body>
<elements-api id="doc" router="hash" hideSchemas="true" />
</body>
<script>
(async() => {
let doc = document.getElementById("doc");
doc.apiDescriptionDocument = {:spec};
})()
</script>
</html>`
const uiRedoc = `
<!DOCTYPE html>
<html>
<head>
<title>Redoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc id="doc"></redoc>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
<script>
(async()=>{
Redoc.init({:spec}, {}, document.getElementById('doc'))
})()
</script>
</body>
</html>`