|
| 1 | +from fastapi import APIRouter, Request |
| 2 | +from fastapi.openapi.docs import get_scalar_api_reference |
| 3 | +from fastapi.responses import HTMLResponse |
| 4 | + |
| 5 | +router = APIRouter(include_in_schema=False) |
| 6 | + |
| 7 | +_ROOT_HTML_TEMPLATE = """\ |
| 8 | +<!DOCTYPE html> |
| 9 | +<html lang="pt-BR"> |
| 10 | +<head> |
| 11 | + <meta charset="UTF-8" /> |
| 12 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 13 | + <title>API Details</title> |
| 14 | + <style> |
| 15 | + *, *::before, *::after {{ box-sizing: border-box; margin: 0; padding: 0; }} |
| 16 | + body {{ |
| 17 | + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; |
| 18 | + background: #f5f5f5; |
| 19 | + display: flex; |
| 20 | + justify-content: center; |
| 21 | + align-items: center; |
| 22 | + min-height: 100vh; |
| 23 | + }} |
| 24 | + .card {{ |
| 25 | + background: #fff; |
| 26 | + border-radius: 12px; |
| 27 | + padding: 2rem 2.5rem; |
| 28 | + box-shadow: 0 4px 24px rgba(0,0,0,.08); |
| 29 | + min-width: 320px; |
| 30 | + }} |
| 31 | + h1 {{ font-size: 1.4rem; color: #1a1a1a; margin-bottom: .25rem; }} |
| 32 | + .version {{ font-size: .8rem; color: #888; margin-bottom: 1.5rem; }} |
| 33 | + .section {{ margin-bottom: 1.25rem; }} |
| 34 | + .section-title {{ |
| 35 | + font-size: .7rem; |
| 36 | + font-weight: 600; |
| 37 | + text-transform: uppercase; |
| 38 | + letter-spacing: .06em; |
| 39 | + color: #999; |
| 40 | + margin-bottom: .5rem; |
| 41 | + }} |
| 42 | + ul {{ list-style: none; }} |
| 43 | + li {{ margin-bottom: .35rem; }} |
| 44 | + a {{ |
| 45 | + display: inline-flex; |
| 46 | + align-items: center; |
| 47 | + gap: .4rem; |
| 48 | + color: #2563eb; |
| 49 | + text-decoration: none; |
| 50 | + font-size: .9rem; |
| 51 | + }} |
| 52 | + a:hover {{ text-decoration: underline; }} |
| 53 | + .badge {{ |
| 54 | + font-size: .65rem; |
| 55 | + background: #e0edff; |
| 56 | + color: #1d4ed8; |
| 57 | + border-radius: 4px; |
| 58 | + padding: 1px 5px; |
| 59 | + font-weight: 600; |
| 60 | + }} |
| 61 | + </style> |
| 62 | +</head> |
| 63 | +<body> |
| 64 | + <div class="card"> |
| 65 | + <h1>Usuarios API Python</h1> |
| 66 | + <p class="version">v{version}</p> |
| 67 | +
|
| 68 | + <div class="section"> |
| 69 | + <div class="section-title">Documentação</div> |
| 70 | + <ul> |
| 71 | + <li><a href="/scalar">Scalar UI <span class="badge">interactive</span></a></li> |
| 72 | + <li><a href="/docs">Swagger UI</a></li> |
| 73 | + <li><a href="/openapi.json">OpenAPI Spec <span class="badge">JSON</span></a></li> |
| 74 | + </ul> |
| 75 | + </div> |
| 76 | +
|
| 77 | + <div class="section"> |
| 78 | + <div class="section-title">Health Probes</div> |
| 79 | + <ul> |
| 80 | + <li><a href="/health/live">GET /health/live</a></li> |
| 81 | + <li><a href="/health/ready">GET /health/ready</a></li> |
| 82 | + </ul> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | +</body> |
| 86 | +</html>""" |
| 87 | + |
| 88 | + |
| 89 | +@router.get("/", response_class=HTMLResponse) |
| 90 | +async def root(request: Request) -> HTMLResponse: |
| 91 | + version = getattr(request.app, "version", "0.1.0") |
| 92 | + return HTMLResponse(content=_ROOT_HTML_TEMPLATE.format(version=version)) |
| 93 | + |
| 94 | + |
| 95 | +@router.get("/scalar", response_class=HTMLResponse) |
| 96 | +async def scalar_ui(request: Request) -> HTMLResponse: |
| 97 | + return get_scalar_api_reference( |
| 98 | + openapi_url=str(request.app.openapi_url), |
| 99 | + title=request.app.title, |
| 100 | + ) |
0 commit comments