Merge branch 'master' into refactor

This commit is contained in:
Daniel G. Taylor 2020-09-10 22:28:29 -07:00
commit 237311f016
No known key found for this signature in database
GPG key ID: 7BD6DC99C9A87E22
3 changed files with 14 additions and 13 deletions

18
docs.go
View file

@ -19,7 +19,7 @@ func splitDocs(docs string) (title, desc string) {
}
// RapiDocHandler renders documentation using RapiDoc.
func RapiDocHandler(pageTitle string) http.Handler {
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>
@ -31,19 +31,19 @@ func RapiDocHandler(pageTitle string) http.Handler {
</head>
<body>
<rapi-doc
spec-url="/openapi.json"
spec-url="%s"
render-style="read"
show-header="false"
primary-color="#f74799"
nav-accent-color="#47afe8"
> </rapi-doc>
</body>
</html>`, pageTitle)))
</html>`, router.GetTitle(), router.OpenAPIPath())))
})
}
// ReDocHandler renders documentation using ReDoc.
func ReDocHandler(pageTitle string) http.Handler {
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>
@ -56,15 +56,15 @@ func ReDocHandler(pageTitle string) http.Handler {
<style>body { margin: 0; padding: 0; }</style>
</head>
<body>
<redoc spec-url='/openapi.json'></redoc>
<redoc spec-url='%s'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
</body>
</html>`, pageTitle)))
</html>`, router.GetTitle(), router.OpenAPIPath())))
})
}
// SwaggerUIHandler renders documentation using Swagger UI.
func SwaggerUIHandler(pageTitle string) http.Handler {
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 -->
@ -106,7 +106,7 @@ func SwaggerUIHandler(pageTitle string) http.Handler {
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/openapi.json",
url: "%s",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
@ -124,6 +124,6 @@ func SwaggerUIHandler(pageTitle string) http.Handler {
}
</script>
</body>
</html>`, pageTitle)))
</html>`, router.GetTitle(), router.OpenAPIPath())))
})
}

View file

@ -12,9 +12,9 @@ var handlers = []struct {
name string
handler http.Handler
}{
{"RapiDoc", RapiDocHandler("Test API")},
{"ReDoc", ReDocHandler("Test API")},
{"SwaggerUI", SwaggerUIHandler("Test API")},
{"RapiDoc", RapiDocHandler(New("Test API", "1.0.0"))},
{"ReDoc", ReDocHandler(New("Test API", "1.0.0"))},
{"SwaggerUI", SwaggerUIHandler(New("Test API", "1.0.0"))},
}
func TestDocHandlers(outer *testing.T) {

View file

@ -255,9 +255,10 @@ func New(docs, version string) *Router {
description: desc,
version: version,
servers: []oaServer{},
docsHandler: RapiDocHandler(title),
}
r.docsHandler = RapiDocHandler(r)
// Error handlers
r.mux.NotFound(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := ContextFromRequest(w, r)