-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.go
62 lines (53 loc) · 1.59 KB
/
swagger.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
package swaggerui
import (
"bytes"
"fmt"
"html/template"
"net/http"
)
// Handler returns a handler using the latest SwaggerUI that loads /swagger.json
func Handler() http.Handler {
return CustomHandler("API Documentation", "/swagger.json", "latest")
}
// CustomHandler returns a handler to render the Swagger UI but allows for custom parameters to be passed
func CustomHandler(title string, specPath string, swaggerUIVersion string) http.Handler {
opts := map[string]string{
"Title": title,
"SwaggerUIScript": fmt.Sprintf("//unpkg.com/swagger-ui-dist@%s/swagger-ui-bundle.js", swaggerUIVersion),
"SwaggerUIStyles": fmt.Sprintf("//unpkg.com/swagger-ui-dist@%s/swagger-ui.css", swaggerUIVersion),
"SpecURL": specPath,
}
tmpl := template.Must(template.New("swaggerUI").Parse(swaggerUITemplate))
buf := bytes.NewBuffer(nil)
_ = tmpl.Execute(buf, opts)
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(buf.Bytes())
}
return http.HandlerFunc(fn)
}
const (
swaggerUITemplate = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{ .Title }}</title>
<script src="{{ .SwaggerUIScript }}"></script>
<link rel="stylesheet" href="{{ .SwaggerUIStyles }}" />
</head>
<body>
<div id="swagger-ui" />
<script>
var ui = SwaggerUIBundle({
url: "{{ .SpecURL }}",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
})
</script>
</body>
</html>
`
)