Files
MyClub/DOCS/DOCS_API_ROUTES.md
T
Tomáš Dvořák 35d0954afd dev day #62
2025-10-16 17:10:13 +02:00

4.3 KiB

Documentation API Routes

Backend Routes Setup

Add these routes to your main.go or router setup:

package main

import (
    "github.com/gin-gonic/gin"
    "your-app/internal/controllers"
    "your-app/internal/middleware"
)

func setupDocsRoutes(router *gin.Engine) {
    // Initialize docs controller
    docsController := controllers.NewDocsController("./DOCS")
    
    // Admin-only documentation routes
    adminDocs := router.Group("/api/v1/admin/docs")
    adminDocs.Use(middleware.RequireAuth())
    adminDocs.Use(middleware.RequireAdmin())
    {
        // Get specific doc file
        adminDocs.GET("/file/*filepath", docsController.GetDocFile)
        
        // List all documentation files
        adminDocs.GET("/list", docsController.ListDocFiles)
        
        // Search documentation
        adminDocs.GET("/search", docsController.SearchDocs)
    }
}

API Endpoints

1. Get Documentation File

GET /api/v1/admin/docs/file/:filepath

Get the content of a specific documentation file.

Parameters:

  • filepath (path) - Path to the markdown file relative to DOCS folder

Example:

GET /api/v1/admin/docs/file/MYUIBRIX_ELEMENTOR_FEATURES.md

Response:

Content-Type: text/markdown

# MyUIbrix Elementor Features
...markdown content...

2. List Documentation Files

GET /api/v1/admin/docs/list

Get a list of all available documentation files.

Response:

{
  "files": [
    {
      "name": "MYUIBRIX_ELEMENTOR_FEATURES.md",
      "path": "/DOCS/MYUIBRIX_ELEMENTOR_FEATURES.md",
      "size": 52480,
      "modified_at": "2024-12-15T10:30:00Z"
    },
    ...
  ],
  "total": 9
}

3. Search Documentation

GET /api/v1/admin/docs/search?q=query

Search through all documentation files.

Parameters:

  • q (query) - Search query string

Example:

GET /api/v1/admin/docs/search?q=inline editor

Response:

{
  "results": [
    {
      "name": "MYUIBRIX_ELEMENTOR_FEATURES.md",
      "path": "/DOCS/MYUIBRIX_ELEMENTOR_FEATURES.md",
      "excerpt": "...inline editor activates...",
      "matches": 5
    }
  ],
  "total": 1,
  "query": "inline editor"
}

Frontend Integration

Update your DevDocsPage.tsx to use the API:

// Load document content from API
const loadDocument = async (docPath: string) => {
  setLoading(true);
  setSelectedDoc(docPath);
  
  try {
    const fileName = docPath.split('/').pop();
    const response = await fetch(`/api/v1/admin/docs/file/${fileName}`, {
      headers: {
        'Authorization': `Bearer ${getToken()}`,
      },
    });
    
    if (!response.ok) throw new Error('Failed to load document');
    
    const content = await response.text();
    setDocContent(content);
  } catch (error) {
    console.error('Error loading document:', error);
    toast({
      title: 'Error loading document',
      status: 'error',
    });
  } finally {
    setLoading(false);
  }
};

Security Considerations

Access Control

  • All documentation routes require authentication
  • Only admin users can access documentation
  • Implements middleware checks

Path Security

  • Prevents directory traversal attacks (.. in paths)
  • Only allows .md files
  • Validates file paths before serving

Implementation

// Prevent directory traversal
if strings.Contains(docPath, "..") {
    c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid file path"})
    return
}

// Only allow markdown files
if !strings.HasSuffix(fullPath, ".md") {
    c.JSON(http.StatusBadRequest, gin.H{"error": "Only markdown files are allowed"})
    return
}

Testing

Test Get File

curl -H "Authorization: Bearer YOUR_TOKEN" \
  http://localhost:8080/api/v1/admin/docs/file/CSS_CLASSES_REFERENCE.md

Test List Files

curl -H "Authorization: Bearer YOUR_TOKEN" \
  http://localhost:8080/api/v1/admin/docs/list
curl -H "Authorization: Bearer YOUR_TOKEN" \
  "http://localhost:8080/api/v1/admin/docs/search?q=custom%20css"

Add to Admin Navigation

Update your admin navigation to include the docs link:

// In AdminLayout.tsx or similar
<NavItem to="/admin/docs" icon={FiBook}>
  Developer Docs
</NavItem>

And add the route:

// In admin routes
<Route path="/admin/docs" element={<DevDocsPage />} />

Status: Complete API Implementation