refactoring

This commit is contained in:
Maxim Devaev 2022-04-05 22:43:53 +03:00
parent 7c4ce1d863
commit 8ce08fb456
2 changed files with 23 additions and 15 deletions

View File

@ -38,6 +38,7 @@ from aiohttp.web import Response
from aiohttp.web import StreamResponse from aiohttp.web import StreamResponse
from aiohttp.web import Application from aiohttp.web import Application
from aiohttp.web import run_app from aiohttp.web import run_app
from aiohttp.web import normalize_path_middleware
try: try:
from aiohttp.web import AccessLogger # type: ignore from aiohttp.web import AccessLogger # type: ignore
@ -230,16 +231,33 @@ class HttpServer:
run_app( run_app(
sock=server_socket, sock=server_socket,
app=self._make_app(), app=self.__make_app(),
shutdown_timeout=1, shutdown_timeout=1,
access_log_format=access_log_format, access_log_format=access_log_format,
print=self.__run_app_print, print=self.__run_app_print,
loop=asyncio.get_event_loop(), loop=asyncio.get_event_loop(),
) )
async def _make_app(self) -> Application: async def _init_app(self, app: Application) -> None:
raise NotImplementedError 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: def __run_app_print(self, text: str) -> None:
logger = get_logger(0) logger = get_logger(0)
for line in text.strip().splitlines(): for line in text.strip().splitlines():

View File

@ -320,15 +320,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
aioproc.rename_process("main") aioproc.rename_process("main")
super().run(**kwargs) super().run(**kwargs)
async def _make_app(self) -> aiohttp.web.Application: async def _init_app(self, app: aiohttp.web.Application) -> None:
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)
self.__run_system_task(self.__stream_controller) self.__run_system_task(self.__stream_controller)
for comp in self.__components: for comp in self.__components:
if comp.systask: 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): for ws_exposed in get_exposed_ws(api):
self.__ws_handlers[ws_exposed.event_type] = ws_exposed.handler self.__ws_handlers[ws_exposed.event_type] = ws_exposed.handler
return app
def __run_system_task(self, method: Callable, *args: Any) -> None: def __run_system_task(self, method: Callable, *args: Any) -> None:
async def wrapper() -> None: async def wrapper() -> None:
try: try:
@ -371,7 +361,7 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
return make_json_exception(err) return make_json_exception(err)
app.router.add_route(exposed.method, exposed.path, wrapper) 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 = get_logger(0)
logger.info("Waiting short tasks ...") 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") 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) logger = get_logger(0)
for comp in self.__components: for comp in self.__components:
if comp.cleanup: if comp.cleanup: