Files
atlas/internal/httpapp/docs_handlers.go

65 lines
1.8 KiB
Go

package httpapp
import (
"net/http"
"os"
"path/filepath"
)
// handleAPIDocs serves the API documentation page
func (a *App) handleAPIDocs(w http.ResponseWriter, r *http.Request) {
// Simple HTML page with Swagger UI
html := `<!DOCTYPE html>
<html>
<head>
<title>AtlasOS API Documentation</title>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@5.10.3/swagger-ui.css" />
<style>
html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
*, *:before, *:after { box-sizing: inherit; }
body { margin:0; background: #fafafa; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.10.3/swagger-ui-bundle.js"></script>
<script src="https://unpkg.com/swagger-ui-dist@5.10.3/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
url: "/api/openapi.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
};
</script>
</body>
</html>`
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(html))
}
// handleOpenAPISpec serves the OpenAPI specification
func (a *App) handleOpenAPISpec(w http.ResponseWriter, r *http.Request) {
// Read OpenAPI spec from file system
specPath := filepath.Join("docs", "openapi.yaml")
spec, err := os.ReadFile(specPath)
if err != nil {
http.Error(w, "OpenAPI spec not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/yaml; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(spec)
}