|
| 1 | +import json |
| 2 | + |
| 3 | +from pydantic import BaseModel |
| 4 | +from starlette.requests import Request |
| 5 | +from starlette.responses import HTMLResponse, JSONResponse |
| 6 | +from starlette.routing import Route |
| 7 | + |
| 8 | +from common import AppConfig |
| 9 | +from common.asyncapi import get_schema |
| 10 | + |
| 11 | + |
| 12 | +class PydanticResponse(JSONResponse): |
| 13 | + def render(self, content: BaseModel) -> bytes: |
| 14 | + return content.model_dump_json( |
| 15 | + exclude_unset=True, |
| 16 | + ).encode("utf-8") |
| 17 | + |
| 18 | + |
| 19 | +async def asyncapi_json(request: Request) -> JSONResponse: |
| 20 | + return PydanticResponse(get_schema()) |
| 21 | + |
| 22 | + |
| 23 | +ASYNCAPI_COMPONENT_VERSION = "latest" |
| 24 | + |
| 25 | +ASYNCAPI_JS_DEFAULT_URL = ( |
| 26 | + f"https://unpkg.com/@asyncapi/react-component@{ASYNCAPI_COMPONENT_VERSION}/browser/standalone/index.js" |
| 27 | +) |
| 28 | +NORMALIZE_CSS_DEFAULT_URL = "https://cdn.jsdelivr.net/npm/modern-normalize/modern-normalize.min.css" |
| 29 | +ASYNCAPI_CSS_DEFAULT_URL = ( |
| 30 | + f"https://unpkg.com/@asyncapi/react-component@{ASYNCAPI_COMPONENT_VERSION}/styles/default.min.css" |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +# https://github.com/asyncapi/asyncapi-react/blob/v2.5.0/docs/usage/standalone-bundle.md |
| 35 | +async def get_asyncapi_html( |
| 36 | + request: Request, |
| 37 | +) -> HTMLResponse: |
| 38 | + app_config = AppConfig() |
| 39 | + """Generate HTML for displaying an AsyncAPI document.""" |
| 40 | + config = { |
| 41 | + "schema": { |
| 42 | + "url": "/docs/asyncapi.json", |
| 43 | + }, |
| 44 | + "config": { |
| 45 | + "show": { |
| 46 | + "sidebar": request.query_params.get("sidebar", "true") == "true", |
| 47 | + "info": request.query_params.get("info", "true") == "true", |
| 48 | + "servers": request.query_params.get("servers", "true") == "true", |
| 49 | + "operations": request.query_params.get("operations", "true") == "true", |
| 50 | + "messages": request.query_params.get("messages", "true") == "true", |
| 51 | + "schemas": request.query_params.get("schemas", "true") == "true", |
| 52 | + "errors": request.query_params.get("errors", "true") == "true", |
| 53 | + }, |
| 54 | + "expand": { |
| 55 | + "messageExamples": request.query_params.get("expand_message_examples") == "true", |
| 56 | + }, |
| 57 | + "sidebar": { |
| 58 | + "showServers": "byDefault", |
| 59 | + "showOperations": "byDefault", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + return HTMLResponse( |
| 65 | + """ |
| 66 | + <!DOCTYPE html> |
| 67 | + <html> |
| 68 | + <head> |
| 69 | + """ |
| 70 | + f""" |
| 71 | + <title>{app_config.APP_NAME} AsyncAPI</title> |
| 72 | + """ |
| 73 | + """ |
| 74 | + <link rel="icon" href="https://www.asyncapi.com/favicon.ico"> |
| 75 | + <link rel="icon" type="image/png" sizes="16x16" href="https://www.asyncapi.com/favicon-16x16.png"> |
| 76 | + <link rel="icon" type="image/png" sizes="32x32" href="https://www.asyncapi.com/favicon-32x32.png"> |
| 77 | + <link rel="icon" type="image/png" sizes="194x194" href="https://www.asyncapi.com/favicon-194x194.png"> |
| 78 | + """ |
| 79 | + f""" |
| 80 | + <link rel="stylesheet" href="{NORMALIZE_CSS_DEFAULT_URL}"> |
| 81 | + <link rel="stylesheet" href="{ASYNCAPI_CSS_DEFAULT_URL}"> |
| 82 | + """ |
| 83 | + """ |
| 84 | + </head> |
| 85 | +
|
| 86 | +
|
| 87 | + <body> |
| 88 | + <div id="asyncapi"></div> |
| 89 | + """ |
| 90 | + f""" |
| 91 | + <script src="{ASYNCAPI_JS_DEFAULT_URL}"></script> |
| 92 | + <script> |
| 93 | + """ |
| 94 | + f""" |
| 95 | + AsyncApiStandalone.render( |
| 96 | + {json.dumps(config)}, |
| 97 | + document.getElementById('asyncapi') |
| 98 | + ); |
| 99 | + """ |
| 100 | + """ |
| 101 | + </script> |
| 102 | + </body> |
| 103 | + </html> |
| 104 | + """ |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +routes = [ |
| 109 | + Route("/asyncapi.json", asyncapi_json, methods=["GET"]), |
| 110 | + Route("/", get_asyncapi_html, methods=["GET"]), |
| 111 | +] |
0 commit comments