-
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.
Signed-off-by: Keming <[email protected]>
- Loading branch information
Showing
4 changed files
with
93 additions
and
4 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
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,53 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | ||
from threading import Thread | ||
|
||
from defspec.template import RenderTemplate | ||
|
||
if sys.version_info >= (3, 11): | ||
from typing import Self | ||
else: | ||
from typing_extensions import Self | ||
|
||
|
||
class OpenAPIHandler(BaseHTTPRequestHandler): | ||
spec: bytes | ||
|
||
@classmethod | ||
def set_openapi_handler(cls, spec: bytes) -> Self: | ||
cls.spec = spec | ||
return cls | ||
|
||
def do_GET(self): | ||
print(self.path) | ||
if self.path == "/openapi/spec.json": | ||
return self.send_spec() | ||
for template in RenderTemplate: | ||
if self.path == f"/openapi/{template.name.lower()}": | ||
return self.send_ui(template.value) | ||
self.send_response(404, "Not Found") | ||
self.end_headers() | ||
|
||
def send_spec(self): | ||
self.send_response(200) | ||
self.send_header("Content-Type", "application/json; charset=utf-8") | ||
self.end_headers() | ||
self.wfile.write(self.spec) | ||
|
||
def send_ui(self, template: str): | ||
content = template.format(spec_url="/openapi/spec.json") | ||
self.send_response(200) | ||
self.send_header("Content-Type", "text/html; charset=utf-8") | ||
self.end_headers() | ||
self.wfile.write(content.encode()) | ||
|
||
|
||
def serve_openapi_http_daemon(host: str, port: int, daemon: bool, spec: bytes): | ||
server = ThreadingHTTPServer((host, port), OpenAPIHandler.set_openapi_handler(spec)) | ||
if daemon: | ||
thread = Thread(target=server.serve_forever, daemon=True) | ||
thread.start() | ||
return | ||
server.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