diff options
author | Maxim Devaev <[email protected]> | 2022-04-05 22:43:53 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2022-04-05 22:43:53 +0300 |
commit | 8ce08fb4567ed7f3cfad85da4ae1e123eef42024 (patch) | |
tree | 3d9f3b38618ffbd1e2927fc4c9606e3ff412eb54 | |
parent | 7c4ce1d86312161021e22947a2bac2649ec879c2 (diff) |
refactoring
-rw-r--r-- | kvmd/apps/kvmd/http.py | 22 | ||||
-rw-r--r-- | kvmd/apps/kvmd/server.py | 16 |
2 files changed, 23 insertions, 15 deletions
diff --git a/kvmd/apps/kvmd/http.py b/kvmd/apps/kvmd/http.py index 797d32dc..a057caed 100644 --- a/kvmd/apps/kvmd/http.py +++ b/kvmd/apps/kvmd/http.py @@ -38,6 +38,7 @@ from aiohttp.web import Response from aiohttp.web import StreamResponse from aiohttp.web import Application from aiohttp.web import run_app +from aiohttp.web import normalize_path_middleware try: from aiohttp.web import AccessLogger # type: ignore @@ -230,16 +231,33 @@ class HttpServer: run_app( sock=server_socket, - app=self._make_app(), + app=self.__make_app(), shutdown_timeout=1, access_log_format=access_log_format, print=self.__run_app_print, loop=asyncio.get_event_loop(), ) - async def _make_app(self) -> Application: + async def _init_app(self, app: Application) -> None: raise NotImplementedError + async def _on_shutdown(self, app: Application) -> None: + _ = app + + async def _on_cleanup(self, app: Application) -> None: + _ = app + + async def __make_app(self) -> Application: + app = Application(middlewares=[normalize_path_middleware( + append_slash=False, + remove_slash=True, + merge_slashes=True, + )]) + app.on_shutdown.append(self._on_shutdown) + app.on_cleanup.append(self._on_cleanup) + await self._init_app(app) + return app + def __run_app_print(self, text: str) -> None: logger = get_logger(0) for line in text.strip().splitlines(): diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index 5f007480..a69cff00 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -320,15 +320,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins aioproc.rename_process("main") super().run(**kwargs) - async def _make_app(self) -> aiohttp.web.Application: - app = aiohttp.web.Application(middlewares=[aiohttp.web.normalize_path_middleware( - append_slash=False, - remove_slash=True, - merge_slashes=True, - )]) - app.on_shutdown.append(self.__on_shutdown) - app.on_cleanup.append(self.__on_cleanup) - + async def _init_app(self, app: aiohttp.web.Application) -> None: self.__run_system_task(self.__stream_controller) for comp in self.__components: if comp.systask: @@ -343,8 +335,6 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins for ws_exposed in get_exposed_ws(api): self.__ws_handlers[ws_exposed.event_type] = ws_exposed.handler - return app - def __run_system_task(self, method: Callable, *args: Any) -> None: async def wrapper() -> None: try: @@ -371,7 +361,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins return make_json_exception(err) app.router.add_route(exposed.method, exposed.path, wrapper) - async def __on_shutdown(self, _: aiohttp.web.Application) -> None: + async def _on_shutdown(self, _: aiohttp.web.Application) -> None: logger = get_logger(0) logger.info("Waiting short tasks ...") @@ -390,7 +380,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins logger.info("On-Shutdown complete") - async def __on_cleanup(self, _: aiohttp.web.Application) -> None: + async def _on_cleanup(self, _: aiohttp.web.Application) -> None: logger = get_logger(0) for comp in self.__components: if comp.cleanup: |