huma/docs.go

130 lines
3.4 KiB
Go
Raw Permalink Normal View History

package huma
import (
"fmt"
2020-08-26 22:25:47 -07:00
"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
}
2020-03-26 21:18:20 -07:00
// RapiDocHandler renders documentation using RapiDoc.
2020-09-10 22:28:29 -07:00
func RapiDocHandler(router *Router) http.Handler {
2020-08-26 22:25:47 -07:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(fmt.Sprintf(`<!doctype html>
2020-03-26 21:18:20 -07:00
<html>
<head>
<title>%s</title>
<meta charset="utf-8">
<script type="module" src="https://unpkg.com/rapidoc@9.1.4/dist/rapidoc-min.js"></script>
2020-03-26 21:18:20 -07:00
</head>
<body>
<rapi-doc
spec-url="%s"
2020-03-26 21:18:20 -07:00
render-style="read"
2020-03-31 23:24:49 -07:00
show-header="false"
primary-color="#f74799"
nav-accent-color="#47afe8"
2020-03-26 21:18:20 -07:00
> </rapi-doc>
</body>
2020-09-10 22:28:29 -07:00
</html>`, router.GetTitle(), router.OpenAPIPath())))
2020-08-26 22:25:47 -07:00
})
2020-03-26 21:18:20 -07:00
}
// ReDocHandler renders documentation using ReDoc.
2020-09-10 22:28:29 -07:00
func ReDocHandler(router *Router) http.Handler {
2020-08-26 22:25:47 -07:00
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>
2020-09-10 22:28:29 -07:00
</html>`, router.GetTitle(), router.OpenAPIPath())))
2020-08-26 22:25:47 -07:00
})
}
2020-03-26 21:18:20 -07:00
// SwaggerUIHandler renders documentation using Swagger UI.
2020-09-10 22:28:29 -07:00
func SwaggerUIHandler(router *Router) http.Handler {
2020-08-26 22:25:47 -07:00
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 -->
2020-03-26 21:18:20 -07:00
<!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",
2020-03-26 21:18:20 -07:00
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>
2020-09-10 22:28:29 -07:00
</html>`, router.GetTitle(), router.OpenAPIPath())))
2020-08-26 22:25:47 -07:00
})
}