first commit
This commit is contained in:
93
internal/http/docs/docs.go
Normal file
93
internal/http/docs/docs.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package docs
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
//go:embed openapi.yaml
|
||||
var openapiYAML []byte
|
||||
|
||||
var openapiJSON []byte
|
||||
|
||||
func init() {
|
||||
var raw any
|
||||
if err := yaml.Unmarshal(openapiYAML, &raw); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
clean := convertMaps(raw)
|
||||
b, err := json.Marshal(clean)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
openapiJSON = b
|
||||
}
|
||||
|
||||
// convertMaps recursively converts map[interface{}]interface{} to map[string]interface{}.
|
||||
func convertMaps(in any) any {
|
||||
switch v := in.(type) {
|
||||
case map[any]any:
|
||||
m := make(map[string]any, len(v))
|
||||
for k, val := range v {
|
||||
m[toString(k)] = convertMaps(val)
|
||||
}
|
||||
return m
|
||||
case map[string]any:
|
||||
m := make(map[string]any, len(v))
|
||||
for k, val := range v {
|
||||
m[k] = convertMaps(val)
|
||||
}
|
||||
return m
|
||||
case []any:
|
||||
out := make([]any, len(v))
|
||||
for i, item := range v {
|
||||
out[i] = convertMaps(item)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
func toString(v any) string {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func ServeJSON(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "public, max-age=300")
|
||||
_, _ = w.Write(openapiJSON)
|
||||
}
|
||||
|
||||
const swaggerHTML = `<!DOCTYPE html>
|
||||
<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" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
||||
<script>
|
||||
window.onload = () => {
|
||||
window.ui = SwaggerUIBundle({
|
||||
url: "/openapi.json",
|
||||
dom_id: "#swagger-ui",
|
||||
deepLinking: true,
|
||||
});
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
func ServeUI(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write([]byte(swaggerHTML))
|
||||
}
|
||||
Reference in New Issue
Block a user