Files
Juan Denis c5710e88b9 Add API keys, analytics, webhooks, and middleware
Introduce programmatic API features and observability: add API Key management, API usage analytics, webhook event subscriptions, and OpenAPI docs. New models (ApiKey, ApiUsageLog, ApiUsageSummary, EventSubscription, EventDelivery), services (api_analytics, api_key, event, openapi), and blueprints (/api-keys, /api-analytics, /event-subscriptions, /docs) were added and registered in create_app. Middleware for API key authentication, buffered analytics logging (with a flush thread), and enhanced rate limiting (custom key function and response headers) were implemented; RBAC was updated to accept API key auth via an auth_required decorator. Background threads for hourly aggregation and event retry were added; security CSP updated to allow resources for Swagger UI. Frontend settings UI and styles were added/updated for API keys and webhooks, and requirements were updated.
2026-03-04 01:53:46 -05:00

48 lines
1.3 KiB
Python

"""OpenAPI/Swagger documentation endpoints."""
from flask import Blueprint, jsonify
docs_bp = Blueprint('docs', __name__)
@docs_bp.route('/', methods=['GET'])
def swagger_ui():
"""Serve Swagger UI."""
html = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ServerKit API Documentation</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
<style>
body { margin: 0; padding: 0; }
.swagger-ui .topbar { display: none; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
<script>
SwaggerUIBundle({
url: './openapi.json',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
layout: 'BaseLayout',
deepLinking: true,
defaultModelsExpandDepth: -1,
});
</script>
</body>
</html>'''
return html, 200, {'Content-Type': 'text/html'}
@docs_bp.route('/openapi.json', methods=['GET'])
def openapi_spec():
"""Return the generated OpenAPI specification."""
from app.services.openapi_service import OpenAPIService
spec = OpenAPIService.generate_spec()
return jsonify(spec)