-
Notifications
You must be signed in to change notification settings - Fork 2
/
redoc.go
133 lines (116 loc) · 3.09 KB
/
redoc.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
package micro
import (
"bytes"
"html/template"
"net/http"
)
// RedocOpts - the Redoc configures type
type RedocOpts struct {
// Route - the route in http server, DO NOT include / at the beginning
Route string
// SpecURLs - the urls to find the spec for, format: name -> url
SpecURLs map[string]string
// RedocURL - the js that generates the redoc site, defaults to: https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js
RedocURL string
// Title - the page title, default to: API documentation
Title string
// Up - whether to boot up the redoc endpoints
Up bool
}
func (redoc *RedocOpts) ensureDefaults() {
if redoc.SpecURLs == nil {
redoc.AddSpec("Service", "/swagger.json")
}
if redoc.RedocURL == "" {
redoc.RedocURL = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"
}
if redoc.Title == "" {
redoc.Title = "API documentation"
}
}
// AddSpec - add a spec url with name
func (redoc *RedocOpts) AddSpec(name, url string) *RedocOpts {
if redoc.SpecURLs == nil {
redoc.SpecURLs = make(map[string]string)
}
redoc.SpecURLs[name] = url
return redoc
}
// Serve - the HandlerFunc for Redoc
func (redoc *RedocOpts) Serve(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
redoc.ensureDefaults()
tmpl := template.Must(template.New("redoc").Parse(redocTemplate))
buf := bytes.NewBuffer(nil)
tmpl.Execute(buf, redoc)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
const (
redocTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding-top: 40px;
}
nav {
position: fixed;
top: 0;
width: 100%;
z-index: 100;
}
#links_container {
margin: 0;
padding: 0;
background-color: #0033a0;
}
#links_container li {
display: inline-block;
padding: 10px;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<!-- Top navigation placeholder -->
<nav>
<ul id="links_container">
</ul>
</nav>
<redoc scroll-y-offset="body > nav"></redoc>
<script src="{{ .RedocURL }}"></script>
<script>
// list of APIS
var apis = [
{{range $key, $value := .SpecURLs}}
{
name: {{ $key }},
url: {{ $value }}
},
{{end}}
];
// initially render first API
Redoc.init(apis[0].url);
function onClick() {
var url = this.getAttribute('data-link');
Redoc.init(url);
}
// dynamically building navigation items
var $list = document.getElementById('links_container');
apis.forEach(function(api) {
var $listitem = document.createElement('li');
$listitem.setAttribute('data-link', api.url);
$listitem.innerText = api.name;
$listitem.addEventListener('click', onClick);
$list.appendChild($listitem);
});
</script>
</body>
</html>
`
)