-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AsyncAPI schema serving with cli command
- Loading branch information
Showing
9 changed files
with
156 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "message-flow" | ||
version = "0.3.3" | ||
version = "0.3.4" | ||
description = "Asynchronous Communication Framework" | ||
authors = [ | ||
{name = "Valentin Vorobyev", email = "[email protected]"}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ...utils import init_package | ||
|
||
init_package(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .request_handler import * | ||
from .server import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import json | ||
from http.server import BaseHTTPRequestHandler | ||
|
||
from ...utils import internal | ||
|
||
|
||
@internal | ||
class RequestHandler(BaseHTTPRequestHandler): | ||
schema: str | ||
|
||
def do_GET(self): | ||
if self.path == "/async-api-docs": | ||
self._send_async_api_schema() | ||
else: | ||
self._send_not_found() | ||
|
||
def _send_async_api_schema(self) -> None: | ||
self._set_headers() | ||
self.wfile.write(self._make_asyncapi_html().encode()) | ||
|
||
def _send_not_found(self) -> None: | ||
self._set_headers(404, "text/plain") | ||
self.wfile.write(b"404 Not Found") | ||
|
||
def _set_headers(self, status: int = 200, content_type: str = "text/html"): | ||
self.send_response(status) | ||
self.send_header("Content-type", content_type) | ||
self.end_headers() | ||
|
||
def _make_asyncapi_html( | ||
self, | ||
) -> str: | ||
config = { | ||
"schema": self.schema, | ||
"config": { | ||
"show": { | ||
"sidebar": True, | ||
"info": True, | ||
"servers": True, | ||
"operations": True, | ||
"messages": True, | ||
"schemas": True, | ||
"errors": True, | ||
}, | ||
"expand": { | ||
"messageExamples": True, | ||
}, | ||
"sidebar": { | ||
"showServers": "byDefault", | ||
"showOperations": "byDefault", | ||
}, | ||
}, | ||
} | ||
|
||
return ( | ||
""" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
""" | ||
f""" | ||
<title>MessageFlow AsyncAPI</title> | ||
""" | ||
""" | ||
<link rel="icon" href="https://www.asyncapi.com/favicon.ico"> | ||
<link rel="icon" type="image/png" sizes="16x16" href="https://www.asyncapi.com/favicon-16x16.png"> | ||
<link rel="icon" type="image/png" sizes="32x32" href="https://www.asyncapi.com/favicon-32x32.png"> | ||
<link rel="icon" type="image/png" sizes="194x194" href="https://www.asyncapi.com/favicon-194x194.png"> | ||
<link rel="stylesheet" href="https://unpkg.com/@asyncapi/[email protected]/styles/default.min.css"> | ||
</head> | ||
<style> | ||
html { | ||
font-family: ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji; | ||
line-height: 1.5; | ||
} | ||
</style> | ||
<body> | ||
<div id="asyncapi"></div> | ||
<script src="https://unpkg.com/@asyncapi/[email protected]/browser/standalone/index.js"></script> | ||
<script> | ||
""" | ||
f""" | ||
AsyncApiStandalone.render({json.dumps(config)}, document.getElementById('asyncapi')); | ||
""" | ||
""" | ||
</script> | ||
</body> | ||
</html> | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from http.server import HTTPServer | ||
|
||
from .request_handler import RequestHandler | ||
|
||
__all__ = ["serve_schema"] | ||
|
||
|
||
def serve_schema( | ||
schema: str, | ||
host: str, | ||
port: int, | ||
) -> None: | ||
RequestHandler.schema = schema | ||
|
||
httpd = HTTPServer((host, port), RequestHandler) | ||
|
||
httpd.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters