Serve Swagger UI from embedded assets instead of unpkg CDN
Bundle swagger-ui-dist@5.32.5 (bundle + CSS) with go:embed and expose /docs/swagger-ui-bundle.js and /docs/swagger-ui.css so /docs works without external script loads (fixes timeouts when unpkg is unreachable). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -11,6 +11,12 @@ import (
|
||||
//go:embed openapi.yaml
|
||||
var openapiYAML []byte
|
||||
|
||||
//go:embed swagger-ui-bundle.js
|
||||
var swaggerUIBundleJS []byte
|
||||
|
||||
//go:embed swagger-ui.css
|
||||
var swaggerUICSS []byte
|
||||
|
||||
var openapiJSON []byte
|
||||
|
||||
func init() {
|
||||
@@ -70,11 +76,11 @@ const swaggerHTML = `<!DOCTYPE html>
|
||||
<head>
|
||||
<title>NIP-05 API</title>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
|
||||
<link rel="stylesheet" href="/docs/swagger-ui.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
||||
<script src="/docs/swagger-ui-bundle.js"></script>
|
||||
<script>
|
||||
window.onload = () => {
|
||||
window.ui = SwaggerUIBundle({
|
||||
@@ -91,3 +97,15 @@ func ServeUI(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write([]byte(swaggerHTML))
|
||||
}
|
||||
|
||||
func ServeJS(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "public, max-age=86400")
|
||||
_, _ = w.Write(swaggerUIBundleJS)
|
||||
}
|
||||
|
||||
func ServeCSS(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "public, max-age=86400")
|
||||
_, _ = w.Write(swaggerUICSS)
|
||||
}
|
||||
|
||||
2
internal/http/docs/swagger-ui-bundle.js
Normal file
2
internal/http/docs/swagger-ui-bundle.js
Normal file
File diff suppressed because one or more lines are too long
3
internal/http/docs/swagger-ui.css
Normal file
3
internal/http/docs/swagger-ui.css
Normal file
File diff suppressed because one or more lines are too long
@@ -62,6 +62,8 @@ func NewServer(d Deps) *http.Server {
|
||||
r.Get("/openapi.json", docs.ServeJSON)
|
||||
r.Get("/docs", docs.ServeUI)
|
||||
r.Get("/docs/", docs.ServeUI)
|
||||
r.Get("/docs/swagger-ui-bundle.js", docs.ServeJS)
|
||||
r.Get("/docs/swagger-ui.css", docs.ServeCSS)
|
||||
|
||||
r.Route("/v1", func(r chi.Router) {
|
||||
r.Get("/pricing", pricing.Handle)
|
||||
|
||||
Reference in New Issue
Block a user