43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package openapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// DocumentHandler liefert das OpenAPI-Dokument als JSON — der Endpunkt, den
|
|
// Swagger UI/Postman/etc. importieren (Akzeptanzkriterium 3).
|
|
func DocumentHandler(doc Document) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(doc)
|
|
}
|
|
}
|
|
|
|
// SwaggerUIHandler liefert eine minimale HTML-Seite, die Swagger UI ueber
|
|
// ein CDN laedt und gegen docURL rendert — Standardmuster fuer
|
|
// "in gaengigen Tools darstellbar" (Akzeptanzkriterium 3), ohne eine eigene
|
|
// Swagger-UI-Distribution einzubetten.
|
|
func SwaggerUIHandler(docURL string) http.HandlerFunc {
|
|
page := fmt.Sprintf(`<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>NEXARCH Core API</title>
|
|
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
|
|
</head>
|
|
<body>
|
|
<div id="swagger-ui"></div>
|
|
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"></script>
|
|
<script>
|
|
window.onload = () => SwaggerUIBundle({ url: %q, dom_id: "#swagger-ui" });
|
|
</script>
|
|
</body>
|
|
</html>`, docURL)
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte(page))
|
|
}
|
|
}
|