def server_process(port, mode, **kwargs): from quart import Quart app = Quart(__name__) @app.route(r'/hello/world', methods=['GET']) async def index_handler(): return 'Hello World!!!' if mode == 'auto': from swagger_ui import api_doc api_doc(app, **kwargs) else: from swagger_ui import quart_api_doc quart_api_doc(app, **kwargs) app.run(host='localhost', port=port)
async def index() -> Response: """Render main web page.""" return await send_file(web_dir / "index.html") @app.route("/swagger.yaml", methods=["GET"]) async def swagger_yaml() -> Response: """OpenAPI static endpoint.""" return await send_file(web_dir / "swagger.yaml") # ----------------------------------------------------------------------------- # Swagger UI quart_api_doc(app, config_path=(web_dir / "swagger.yaml"), url_prefix="/api", title="Rhasspy API") # ----------------------------------------------------------------------------- def prefers_json() -> bool: """True if client prefers JSON over plain text.""" return quality(request.accept_mimetypes, "application/json") > quality( request.accept_mimetypes, "text/plain") def quality(accept, key: str) -> float: """Return Accept quality for media type.""" for option in accept.options: # pylint: disable=W0212
@app.route("/img/<path:filename>", methods=["GET"]) async def img(filename) -> Response: """Image static endpoint.""" return await send_from_directory(_IMG_DIR, filename) @app.route("/wav/<path:filename>", methods=["GET"]) async def wav(filename) -> Response: """WAV audio static endpoint.""" return await send_from_directory(_WAV_DIR, filename) # Swagger UI quart_api_doc(app, config_path=str(_DIR / "swagger.yaml"), url_prefix="/openapi", title="Larynx") @app.errorhandler(Exception) async def handle_error(err) -> typing.Tuple[str, int]: """Return error as text.""" _LOGGER.exception(err) return (f"{err.__class__.__name__}: {err}", 500) # ----------------------------------------------------------------------------- # Run Web Server # ----------------------------------------------------------------------------- hyp_config = hypercorn.config.Config()
@app.route("/css/<path:filename>", methods=["GET"]) async def css(filename) -> Response: """CSS static endpoint.""" return await send_from_directory("css", filename) @app.route("/img/<path:filename>", methods=["GET"]) async def img(filename) -> Response: """Image static endpoint.""" return await send_from_directory("img", filename) # Swagger UI quart_api_doc(app, config_path="swagger.yaml", url_prefix="/api", title="OpenTTS") @app.errorhandler(Exception) async def handle_error(err) -> typing.Tuple[str, int]: """Return error as text.""" _LOGGER.exception(err) return (f"{err.__class__.__name__}: {err}", 500) # ----------------------------------------------------------------------------- app.run(host="0.0.0.0", port=5500)