📖 Docs: Add /docs/markdown endpoint to output markdown API documentation
Browse files
main.py
CHANGED
|
@@ -101,6 +101,39 @@ async def lifespan(app: FastAPI):
|
|
| 101 |
|
| 102 |
app = FastAPI(lifespan=lifespan, debug=is_debug)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
@app.exception_handler(HTTPException)
|
| 105 |
async def http_exception_handler(request: Request, exc: HTTPException):
|
| 106 |
if exc.status_code == 404:
|
|
|
|
| 101 |
|
| 102 |
app = FastAPI(lifespan=lifespan, debug=is_debug)
|
| 103 |
|
| 104 |
+
def generate_markdown_docs():
|
| 105 |
+
openapi_schema = app.openapi()
|
| 106 |
+
|
| 107 |
+
markdown = f"# {openapi_schema['info']['title']}\n\n"
|
| 108 |
+
markdown += f"Version: {openapi_schema['info']['version']}\n\n"
|
| 109 |
+
markdown += f"{openapi_schema['info'].get('description', '')}\n\n"
|
| 110 |
+
|
| 111 |
+
markdown += "## API Endpoints\n\n"
|
| 112 |
+
|
| 113 |
+
paths = openapi_schema['paths']
|
| 114 |
+
for path, path_info in paths.items():
|
| 115 |
+
for method, operation in path_info.items():
|
| 116 |
+
markdown += f"### {method.upper()} {path}\n\n"
|
| 117 |
+
markdown += f"{operation.get('summary', '')}\n\n"
|
| 118 |
+
markdown += f"{operation.get('description', '')}\n\n"
|
| 119 |
+
|
| 120 |
+
if 'parameters' in operation:
|
| 121 |
+
markdown += "Parameters:\n"
|
| 122 |
+
for param in operation['parameters']:
|
| 123 |
+
markdown += f"- {param['name']} ({param['in']}): {param.get('description', '')}\n"
|
| 124 |
+
|
| 125 |
+
markdown += "\n---\n\n"
|
| 126 |
+
|
| 127 |
+
return markdown
|
| 128 |
+
|
| 129 |
+
@app.get("/docs/markdown")
|
| 130 |
+
async def get_markdown_docs():
|
| 131 |
+
markdown = generate_markdown_docs()
|
| 132 |
+
return Response(
|
| 133 |
+
content=markdown,
|
| 134 |
+
media_type="text/markdown"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
@app.exception_handler(HTTPException)
|
| 138 |
async def http_exception_handler(request: Request, exc: HTTPException):
|
| 139 |
if exc.status_code == 404:
|