docs: integrate API explorer into documentation pages

Phase 3 complete: Add API explorer to endpoint documentation
- Add DocsLayoutWithExplorer template function
- Update HTTPHandler.ShowAPIEndpoint to check authentication
- Add GetAPIEndpointData method to docs handler
- Include API explorer for all endpoint documentation
- Explorer shows mock data to non-authenticated users
- Explorer enables real API execution for logged-in users
- Add legacy fallback for endpoints without explorer data
This commit is contained in:
2026-02-02 08:58:12 -05:00
parent afdcc7b589
commit 3b3630666a
4 changed files with 800 additions and 0 deletions
+66
View File
@@ -80,6 +80,72 @@ func (h *HTTPHandler) ShowDocumentation(c echo.Context) error {
// ShowAPIEndpoint shows a specific API endpoint documentation
func (h *HTTPHandler) ShowAPIEndpoint(c echo.Context, endpointPath string) error {
// Check if user is logged in
isLoggedIn := false
if userID := c.Get("user_id"); userID != nil {
isLoggedIn = true
}
// Get endpoint data for the explorer
endpointInfo, err := h.docs.GetAPIEndpointData(endpointPath)
if err != nil {
// Endpoint not found in explorer data, fall back to old behavior
return h.showLegacyAPIEndpoint(c, endpointPath, isLoggedIn)
}
// Load the markdown documentation for this endpoint
docPath := "api/" + endpointPath + ".md"
doc, err := h.docs.LoadDocument(docPath)
if err != nil {
errorHTML := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>API Endpoint Not Found - Bookhoard</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen flex items-center justify-center">
<div class="text-center">
<h1 class="text-2xl font-bold mb-4">API Endpoint Not Found</h1>
<a href="/docs/api" class="text-blue-400 hover:underline">Return to API Documentation</a>
</div>
</body>
</html>`
return c.HTML(http.StatusNotFound, errorHTML)
}
// Get navigation
nav := h.docs.BuildNavigation()
// Create empty user (docs are public)
user := templates.User{
ID: "",
Username: "",
Email: "",
Role: "",
Theme: "tokyo-night",
}
// Create API explorer data
explorerData := templates.APIExplorerData{
Endpoint: *endpointInfo,
IsLoggedIn: isLoggedIn,
}
// Render API endpoint page with explorer
var buf bytes.Buffer
err = templates.DocsLayoutWithExplorer(*nav, *doc, user, explorerData).Render(c.Request().Context(), &buf)
if err != nil {
return c.HTML(http.StatusInternalServerError, "Failed to render page")
}
return c.HTML(http.StatusOK, buf.String())
}
// showLegacyAPIEndpoint shows API endpoint documentation without explorer data
func (h *HTTPHandler) showLegacyAPIEndpoint(c echo.Context, endpointPath string, isLoggedIn bool) error {
endpoints := h.docs.GetAPIEndpoints()
// Find the endpoint