-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathswagger.go
More file actions
325 lines (285 loc) · 9.7 KB
/
Copy pathswagger.go
File metadata and controls
325 lines (285 loc) · 9.7 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package xmux
import (
"bytes"
"encoding/json"
"log"
"html/template"
"net/http"
"strings"
)
type MethodStrcut struct {
Summary string `json:"summary,omitempty" yaml:"summary"`
Parameters []Parameter `json:"parameters,omitempty" yaml:"parameters"`
Responses map[string]SwaggerResponse `json:"responses,omitempty" yaml:"responses"`
Produces []string `json:"produces,omitempty" yaml:"produces" required:""`
Consumes []string `json:"consumes,omitempty" yaml:"consumes"`
}
type ParameterType string
const (
Query ParameterType = "query"
Path ParameterType = "path"
Header ParameterType = "header"
Form ParameterType = "formData"
)
type Parameter struct {
In ParameterType `json:"in,omitempty" yaml:"in"`
Name string `json:"name,omitempty" yaml:"name"`
Required bool `required:"in,omitempty" yaml:"required"`
Type string `json:"type,omitempty" yaml:"type"`
Schema map[string]string `json:"schema,omitempty" yaml:"schema"`
Minimum int64 `json:"minimum,omitempty" yaml:"minimum"`
Enum []string `json:"enum,omitempty" yaml:"enum"`
Default any `json:"default,omitempty" yaml:"default"`
Description string `json:"description,omitempty" yaml:"description"`
}
type Schema struct {
Type string `json:"type" yaml:"type"`
Properties map[string]Type `json:"properties" yaml:"properties"` // key是字段名
Ref string `json:"$ref" yaml:"$ref"` // $ref: '#/definitions/User'
}
type Type struct {
Type string `json:"type" yaml:"type"`
Description string `json:"description" yaml:"description"`
}
type SwaggerResponse struct {
Description string `json:"type,omitempty" yaml:"type"`
Schema map[string]string `json:"schema,omitempty" yaml:"schema"`
}
type Info struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Version string `json:"version" yaml:"version"`
}
type Swagger struct {
Swagger string `json:"swagger" yaml:"swagger"`
Info Info `json:"info" yaml:"info"`
Host string `json:"host" yaml:"host"`
BasePath string `json:"basePath,omitempty" yaml:"basePath"`
Schemes []string `json:"schemes,omitempty" yaml:"schemes"`
Paths map[string]map[string]MethodStrcut `json:"paths,omitempty" yaml:"paths"`
Definitions map[string]Definition `json:"definitions,omitempty" yaml:"definitions"`
Security []map[string][]string `json:"security,omitempty" yaml:"security"`
SecurityDefinitions map[string]Type `json:"securityDefinitions,omitempty" yaml:"securityDefinitions"`
}
type Definition struct {
Properties map[string]Type `json:"properties" yaml:"properties"`
Required []string `json:"required" yaml:"required"`
}
type SwaggerUIOpts struct {
// BasePath for the UI path, defaults to: /
SpecURL string
// The three components needed to embed swagger-ui
SwaggerURL string
SwaggerPresetURL string
SwaggerStylesURL string
Favicon32 string
Favicon16 string
// Title for the documentation site, default to: API documentation
Title string
}
func (r *router) ShowSwagger(url, host string, schemes ...string) *RouteGroup {
jsonPath := "/swagger.json"
swagger := NewRouteGroup().BindResponse(nil).SetHeader("Access-Control-Allow-Origin", "*")
swagger.SetHeader("Content-Type", "sec-ch-ua;sec-ch-ua-mobile;sec-ch-ua-platform")
swagger.SetHeader("Access-Control-Allow-Methods", "*")
swagger.Get(url, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
opts := DefaultEnsure(jsonPath)
tmpl := template.Must(template.New("swaggerui").Parse(swaggeruiTemplate))
buf := bytes.NewBuffer(nil)
_ = tmpl.Execute(buf, &opts)
w.Write(buf.Bytes())
})
swagger.Get(jsonPath, JsonFile(jsonPath, url, host, r, schemes...))
return swagger
}
func JsonFile(jsonPath, url, host string, router *router, schemes ...string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 拿到路由
ss := schemes
if len(schemes) == 0 {
ss = []string{"http"}
}
swagger := Swagger{
Swagger: "2.0",
Host: host,
Info: Info{
Title: router.SwaggerTitle,
Version: router.SwaggerVersion,
Description: router.SwaggerDescription,
},
Schemes: ss,
Paths: make(map[string]map[string]MethodStrcut),
}
// 合并匹配请求
for k, mr := range router.urlRoute {
if k == url || k == jsonPath {
continue
}
ms := MethodStrcut{
Summary: mr.summary,
Produces: []string{"application/json"},
Responses: map[string]SwaggerResponse{"200": {
Description: "",
}},
}
path := make(map[string]MethodStrcut)
for _, method := range mr.methods {
path[strings.ToLower(method)] = ms
}
if acc, ok := mr.header["Content-Type"]; ok {
ms.Produces = strings.Split(acc, ";")
} else {
ms.Produces = []string{"application/json"}
}
ms.Parameters = mr.query
swagger.Paths[k] = path
}
// 合并正则请求
for url, mr := range router.urlTpl {
path := make(map[string]MethodStrcut)
for _, method := range mr.methods {
ms := MethodStrcut{
Summary: mr.summary,
Produces: []string{"application/json"},
Responses: map[string]SwaggerResponse{"200": {
Description: "",
}},
}
if acc, ok := mr.header["Content-Type"]; ok {
ms.Produces = strings.Split(acc, ";")
} else {
ms.Produces = []string{"application/json"}
}
ms.Parameters = mr.query
// 正则请求还需要合并path, 自定义正则必须每个组都以^开头 $结尾, 不然无法自动生成
for _, name := range mr.params {
// url 进行填充
// 将url的^ 替换成 { 将url的$ 替换成 }
url = url[1 : len(url)-1]
start := strings.Index(url, "(")
end := strings.Index(url, ")")
t := "string"
if url[start+1:end] == interger {
t = "interger"
}
// 将里面的值替换
url = url[:start] + "{" + name + "}" + url[end+1:]
p := Parameter{
In: Path,
Name: name,
Required: true,
Type: t,
}
ms.Parameters = append(ms.Parameters, p)
}
path[strings.ToLower(method)] = ms
swagger.Paths[url] = path
}
}
send, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
log.Fatal(err)
}
w.Write(send)
}
}
func DefaultEnsure(jsonPath string) *SwaggerUIOpts {
return &SwaggerUIOpts{
SpecURL: jsonPath,
SwaggerURL: swaggerLatest,
SwaggerPresetURL: swaggerPresetLatest,
SwaggerStylesURL: swaggerStylesLatest,
Favicon16: swaggerFavicon16Latest,
Favicon32: swaggerFavicon32Latest,
Title: "API documentation",
}
}
// SwaggerUI creates a middleware to serve a documentation site for a swagger spec.
// This allows for altering the spec before starting the http listener.
// func SwaggerUI(opts SwaggerUIOpts, next http.Handler) http.Handler {
// opts.EnsureDefaults()
// pth := path.Join(opts.BasePath, opts.Path)
// tmpl := template.Must(template.New("swaggerui").Parse(swaggeruiTemplate))
// buf := bytes.NewBuffer(nil)
// _ = tmpl.Execute(buf, &opts)
// b := buf.Bytes()
// return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// if r.URL.Path == pth {
// rw.Header().Set("Content-Type", "text/html; charset=utf-8")
// rw.WriteHeader(http.StatusOK)
// _, _ = rw.Write(b)
// return
// }
// if next == nil {
// rw.Header().Set("Content-Type", "text/plain")
// rw.WriteHeader(http.StatusNotFound)
// _, _ = rw.Write([]byte(fmt.Sprintf("%q not found", pth)))
// return
// }
// next.ServeHTTP(rw, r)
// })
// }
const (
swaggerLatest = "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"
swaggerPresetLatest = "https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js"
swaggerStylesLatest = "https://unpkg.com/swagger-ui-dist/swagger-ui.css"
swaggerFavicon32Latest = "https://unpkg.com/swagger-ui-dist/favicon-32x32.png"
swaggerFavicon16Latest = "https://unpkg.com/swagger-ui-dist/favicon-16x16.png"
swaggeruiTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="{{ .SwaggerStylesURL }}" >
<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="{{ .SwaggerURL }}"> </script>
<script src="{{ .SwaggerPresetURL }}"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: '{{ .SpecURL }}',
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>
`
)