This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocs.go
130 lines (120 loc) · 3.41 KB
/
docs.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
package huma
import (
"fmt"
"net/http"
"strings"
)
// splitDocs will split a single string out into a title/description combo.
func splitDocs(docs string) (title, desc string) {
title = docs
desc = ""
if strings.Contains(docs, "\n") {
parts := strings.SplitN(docs, "\n", 2)
title = parts[0]
desc = parts[1]
}
return
}
// RapiDocHandler renders documentation using RapiDoc.
func RapiDocHandler(router *Router) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(fmt.Sprintf(`<!doctype html>
<html>
<head>
<title>%s</title>
<meta charset="utf-8">
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
</head>
<body>
<rapi-doc
spec-url="%s"
render-style="read"
show-header="false"
primary-color="#f74799"
nav-accent-color="#47afe8"
schema-description-expanded=true
> </rapi-doc>
</body>
</html>`, router.GetTitle(), router.OpenAPIPath())))
})
}
// ReDocHandler renders documentation using ReDoc.
func ReDocHandler(router *Router) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<title>%s</title>
<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 spec-url='%s'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
</body>
</html>`, router.GetTitle(), router.OpenAPIPath())))
})
}
// SwaggerUIHandler renders documentation using Swagger UI.
func SwaggerUIHandler(router *Router) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(fmt.Sprintf(`<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>%s</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui.css" >
<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="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui-bundle.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.24.2/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "%s",
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>`, router.GetTitle(), router.OpenAPIPath())))
})
}