54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
_ "embed"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed openapi.json
|
|
var openAPIDocument []byte
|
|
|
|
const swaggerUI = `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Card to IBAN API documentation</title>
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css">
|
|
</head>
|
|
<body>
|
|
<div id="swagger-ui"></div>
|
|
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
|
<script>
|
|
window.onload = () => SwaggerUIBundle({
|
|
url: "/openapi.json",
|
|
dom_id: "#swagger-ui",
|
|
deepLinking: true,
|
|
presets: [SwaggerUIBundle.presets.apis]
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
// RegisterDocumentation registers the OpenAPI document and its Swagger UI.
|
|
func RegisterDocumentation(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /openapi.json", serveOpenAPI)
|
|
mux.HandleFunc("GET /swagger/", serveSwaggerUI)
|
|
mux.HandleFunc("GET /docs", redirectToSwaggerUI)
|
|
}
|
|
|
|
func serveOpenAPI(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(openAPIDocument)
|
|
}
|
|
|
|
func serveSwaggerUI(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte(swaggerUI))
|
|
}
|
|
|
|
func redirectToSwaggerUI(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "/swagger/", http.StatusTemporaryRedirect)
|
|
}
|