55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Generate OpenAPI specification from FastAPI app.
|
|
|
|
This script imports the FastAPI app and exports its OpenAPI schema to a JSON file.
|
|
"""
|
|
import json
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path to import memory module
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from memora.web.server import create_app
|
|
from memora import TemporalSemanticMemory
|
|
|
|
def generate_openapi_spec(output_path: str = "openapi.json"):
|
|
"""Generate OpenAPI spec and save to file."""
|
|
# Create a temporary memory instance for OpenAPI generation
|
|
_memory = TemporalSemanticMemory(
|
|
db_url=os.getenv("DATABASE_URL", "postgresql://user:pass@localhost:5432/memora"),
|
|
memory_llm_provider=os.getenv("MEMORY_LLM_PROVIDER", "groq"),
|
|
memory_llm_api_key=os.getenv("MEMORY_LLM_API_KEY", "dummy"),
|
|
memory_llm_model=os.getenv("MEMORY_LLM_MODEL", "openai/gpt-oss-120b"),
|
|
memory_llm_base_url=os.getenv("MEMORY_LLM_BASE_URL") or None,
|
|
)
|
|
app = create_app(_memory)
|
|
|
|
# Get the OpenAPI schema from the app
|
|
openapi_schema = app.openapi()
|
|
|
|
# Write to file
|
|
output_file = Path(output_path)
|
|
with open(output_file, 'w') as f:
|
|
json.dump(openapi_schema, f, indent=2)
|
|
|
|
print(f"✓ OpenAPI specification generated: {output_file.absolute()}")
|
|
print(f" - Title: {openapi_schema['info']['title']}")
|
|
print(f" - Version: {openapi_schema['info']['version']}")
|
|
print(f" - Endpoints: {len(openapi_schema['paths'])}")
|
|
|
|
# List endpoints
|
|
print("\n Endpoints:")
|
|
for path, methods in openapi_schema['paths'].items():
|
|
for method in methods.keys():
|
|
if method.upper() in ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']:
|
|
endpoint_info = methods[method]
|
|
summary = endpoint_info.get('summary', 'No summary')
|
|
tags = ', '.join(endpoint_info.get('tags', ['untagged']))
|
|
print(f" {method.upper():6} {path:30} [{tags}] - {summary}")
|
|
|
|
if __name__ == "__main__":
|
|
output = sys.argv[1] if len(sys.argv) > 1 else "openapi.json"
|
|
generate_openapi_spec(output)
|