mirror of
https://github.com/danielmiessler/Fabric.git
synced 2026-01-08 22:08:03 -05:00
- Add Swagger UI at `/swagger/index.html` endpoint - Generate OpenAPI spec files (JSON and YAML) - Document chat, patterns, and models endpoints - Update contributing guide with Swagger annotation instructions - Add swaggo dependencies to project - Configure authentication bypass for Swagger documentation - Add custom YAML handler for OpenAPI spec - Update REST API documentation with Swagger links - Add dictionary entries for new tools
39 lines
950 B
Go
39 lines
950 B
Go
package restapi
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const APIKeyHeader = "X-API-Key"
|
|
|
|
// APIKeyMiddleware validates API key for protected endpoints.
|
|
// Swagger documentation endpoints (/swagger/*) are exempt from authentication
|
|
// to allow users to browse and test the API documentation freely.
|
|
func APIKeyMiddleware(apiKey string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Skip authentication for Swagger documentation endpoints
|
|
// This allows public access to API docs even when authentication is enabled
|
|
if strings.HasPrefix(c.Request.URL.Path, "/swagger/") {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
headerApiKey := c.GetHeader(APIKeyHeader)
|
|
|
|
if headerApiKey == "" {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Missing API Key"})
|
|
return
|
|
}
|
|
|
|
if headerApiKey != apiKey {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Wrong API Key"})
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|