Commit ·
6d86fc0
1
Parent(s): 5de7a1a
feat(api): enhance root endpoint with markdown rendering
Browse filesThe root endpoint now reads and converts the README.md file into HTML, providing a more informative and visually appealing response. This includes:
- Reading the README.md file and converting its Markdown content to HTML.
- Applying custom styles from main.css and syntax highlighting using Pygments.
- Returning an HTML response with the rendered Markdown content.
Additionally, a new main.css file has been added to style the HTML content, ensuring a consistent and clean presentation.
ref #123: enhance root endpoint with markdown rendering
main.css
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.markdown-body {
|
| 2 |
+
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI";
|
| 3 |
+
max-width: 800px;
|
| 4 |
+
margin: 0 auto;
|
| 5 |
+
padding: 2rem;
|
| 6 |
+
color: #24292e;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
.markdown-body h1, .markdown-body h2 {
|
| 10 |
+
border-bottom: 1px solid #eaecef;
|
| 11 |
+
padding-bottom: .3em;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
.markdown-body pre {
|
| 15 |
+
padding: 16px;
|
| 16 |
+
overflow: auto;
|
| 17 |
+
background-color: #f6f8fa;
|
| 18 |
+
border-radius: 3px;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.markdown-body table {
|
| 22 |
+
border-collapse: collapse;
|
| 23 |
+
width: 100%;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
.markdown-body table th,
|
| 27 |
+
.markdown-body table td {
|
| 28 |
+
padding: 6px 13px;
|
| 29 |
+
border: 1px solid #dfe2e5;
|
| 30 |
+
}
|
main.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import json
|
| 2 |
import logging
|
| 3 |
-
import os
|
| 4 |
import time
|
| 5 |
from concurrent.futures import ThreadPoolExecutor
|
| 6 |
from contextvars import ContextVar
|
|
@@ -11,6 +10,9 @@ from fastapi import FastAPI, HTTPException, Request, Response
|
|
| 11 |
from fastapi.responses import JSONResponse, StreamingResponse, HTMLResponse
|
| 12 |
from fastapi.security import HTTPBearer
|
| 13 |
from starlette.concurrency import run_in_threadpool
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
from schemas import OpenAIChatCompletionForm, FilterForm
|
|
@@ -121,7 +123,59 @@ def get_anthropic_client():
|
|
| 121 |
@app.get("/")
|
| 122 |
async def read_root():
|
| 123 |
"""Root endpoint for API health check."""
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
@app.get("/v1/models")
|
|
|
|
| 1 |
import json
|
| 2 |
import logging
|
|
|
|
| 3 |
import time
|
| 4 |
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
from contextvars import ContextVar
|
|
|
|
| 10 |
from fastapi.responses import JSONResponse, StreamingResponse, HTMLResponse
|
| 11 |
from fastapi.security import HTTPBearer
|
| 12 |
from starlette.concurrency import run_in_threadpool
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
import markdown
|
| 15 |
+
from pygments.formatters import HtmlFormatter
|
| 16 |
|
| 17 |
|
| 18 |
from schemas import OpenAIChatCompletionForm, FilterForm
|
|
|
|
| 123 |
@app.get("/")
|
| 124 |
async def read_root():
|
| 125 |
"""Root endpoint for API health check."""
|
| 126 |
+
try:
|
| 127 |
+
# Lecture du README.md
|
| 128 |
+
readme_path = Path("README.md")
|
| 129 |
+
if not readme_path.exists():
|
| 130 |
+
return HTMLResponse(content="<h1>README.md non trouvé</h1>")
|
| 131 |
+
|
| 132 |
+
md_text = readme_path.read_text(encoding='utf-8')
|
| 133 |
+
|
| 134 |
+
# Conversion Markdown vers HTML
|
| 135 |
+
html = markdown.markdown(
|
| 136 |
+
md_text,
|
| 137 |
+
extensions=[
|
| 138 |
+
'markdown.extensions.fenced_code',
|
| 139 |
+
'markdown.extensions.tables',
|
| 140 |
+
'markdown.extensions.codehilite',
|
| 141 |
+
'markdown.extensions.sane_lists'
|
| 142 |
+
]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Lecture du CSS
|
| 146 |
+
css_file = Path("main.css")
|
| 147 |
+
custom_css = css_file.read_text(encoding='utf-8') if css_file.exists() else ""
|
| 148 |
+
|
| 149 |
+
# CSS pour la coloration syntaxique
|
| 150 |
+
code_css = HtmlFormatter(style='github').get_style_defs('.codehilite')
|
| 151 |
+
|
| 152 |
+
# Construction de la page HTML
|
| 153 |
+
html_content = f"""
|
| 154 |
+
<!DOCTYPE html>
|
| 155 |
+
<html>
|
| 156 |
+
<head>
|
| 157 |
+
<meta charset="utf-8">
|
| 158 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 159 |
+
<style>
|
| 160 |
+
{custom_css}
|
| 161 |
+
{code_css}
|
| 162 |
+
</style>
|
| 163 |
+
</head>
|
| 164 |
+
<body>
|
| 165 |
+
<div class="markdown-body">
|
| 166 |
+
{html}
|
| 167 |
+
</div>
|
| 168 |
+
</body>
|
| 169 |
+
</html>
|
| 170 |
+
"""
|
| 171 |
+
|
| 172 |
+
return HTMLResponse(content=html_content)
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return HTMLResponse(
|
| 176 |
+
content=f"<h1>Erreur: {str(e)}</h1>",
|
| 177 |
+
status_code=500
|
| 178 |
+
)
|
| 179 |
|
| 180 |
|
| 181 |
@app.get("/v1/models")
|