From fe7edb65a89994ee767ea9c8f290d3ef147ff0c6 Mon Sep 17 00:00:00 2001 From: Chris Fiege Date: Thu, 26 May 2022 21:27:30 +0200 Subject: [PATCH] feat(LonaServer): Allow to set aiohttp client_max_size With this change it is now possible to set the aiohttp ``client_max_size`` during the creation of the ``Application``. This value controls the maximum size of a POST request in bytes. This change basically moves the creation of the aiohttp ``Application`` from ``run_server.py`` and ``app.py`` into the ``LonaServer``. Signed-off-by: Chris Fiege --- .../end-user-documentation/settings.rst | 14 +++++++++ lona/app.py | 4 +-- lona/command_line/run_server.py | 8 ++--- lona/default_settings.py | 3 ++ lona/pytest.py | 5 +-- lona/server.py | 31 ++++++++++++------- 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/doc/content/end-user-documentation/settings.rst b/doc/content/end-user-documentation/settings.rst index 97933b95..471cfb50 100644 --- a/doc/content/end-user-documentation/settings.rst +++ b/doc/content/end-user-documentation/settings.rst @@ -224,3 +224,17 @@ Testing .. setting:: :name: TEST_INPUT_EVENT_TIMEOUT :path: lona.default_settings.TEST_INPUT_EVENT_TIMEOUT + +Server +------ + +.. setting:: + :name: AIOHTTP_CLIENT_MAX_SIZE + :path: lona.default_settings.AIOHTTP_CLIENT_MAX_SIZE + + This value is used to set the ``client_max_size`` value for the aiohttp server. + It defines the maximum body size of a post request accepted by the server. + See + `aiohttp documentation `_ + for details. + The default value is set to the aiohttp default of ``1024**2`` Bytes. diff --git a/lona/app.py b/lona/app.py index 39a3ca2f..5461e69c 100644 --- a/lona/app.py +++ b/lona/app.py @@ -396,7 +396,6 @@ def setup_server( self.settings.STATIC_DIRS.insert(0, self.static_dir) # setup server - self.aiohttp_app = Application(loop=loop) settings_post_overrides = { **self._get_settings_as_dict(), @@ -404,12 +403,12 @@ def setup_server( } self.server = LonaServer( - app=self.aiohttp_app, project_root=self.project_root, settings_pre_overrides=settings_pre_overrides, settings_post_overrides=settings_post_overrides, routes=self.routes, ) + self.aiohttp_app = self.server._app # setup worker pool worker_pool = WorkerPool( @@ -462,6 +461,5 @@ def run( # start server run_server( args=server_args, - app=self.aiohttp_app, server=self.server, ) diff --git a/lona/command_line/run_server.py b/lona/command_line/run_server.py index 69d81340..29982645 100644 --- a/lona/command_line/run_server.py +++ b/lona/command_line/run_server.py @@ -4,7 +4,7 @@ import signal import os -from aiohttp.web import Application, run_app +from aiohttp.web import run_app import aiohttp from lona.shell.shell import generate_shell_server, embed_shell @@ -19,22 +19,20 @@ logger = logging.getLogger('lona') -def run_server(args, app=None, server=None): +def run_server(args, server=None): loop = asyncio.get_event_loop() # setup logging log_formatter, log_filter = setup_logging(args) # setup lona server - app = app or Application() - server = server or LonaServer( - app=app, project_root=args.project_root, settings_paths=args.settings, settings_pre_overrides=args.settings_pre_overrides, settings_post_overrides=args.settings_post_overrides, ) + app = server._app worker_pool = WorkerPool( settings=server.settings, diff --git a/lona/default_settings.py b/lona/default_settings.py index bcba4d7b..5c0830b5 100644 --- a/lona/default_settings.py +++ b/lona/default_settings.py @@ -94,3 +94,6 @@ # testing TEST_VIEW_START_TIMEOUT = False TEST_INPUT_EVENT_TIMEOUT = False + +# server +AIOHTTP_CLIENT_MAX_SIZE = 1024**2 diff --git a/lona/pytest.py b/lona/pytest.py index 0b214fd7..93070174 100644 --- a/lona/pytest.py +++ b/lona/pytest.py @@ -150,11 +150,8 @@ async def setup_lona_project_context( def setup_aiohttp_app(loop: AbstractEventLoop) -> Application: nonlocal server - aiohttp_app = Application(loop=loop) - # setup lona server server = LonaServer( - app=aiohttp_app, project_root=project_root or request.fspath, settings_paths=settings or [], settings_pre_overrides=settings_pre_overrides or {}, @@ -164,7 +161,7 @@ def setup_aiohttp_app(loop: AbstractEventLoop) -> Application: server._loop = loop server._worker_pool = WorkerPool(settings=server.settings) - return aiohttp_app + return server._app client = await aiohttp_client(setup_aiohttp_app) diff --git a/lona/server.py b/lona/server.py index eac9bc10..d773ad8e 100644 --- a/lona/server.py +++ b/lona/server.py @@ -12,7 +12,13 @@ import asyncio import os -from aiohttp.web import WebSocketResponse, FileResponse, HTTPFound, Response +from aiohttp.web import ( + WebSocketResponse, + FileResponse, + Application, + HTTPFound, + Response, +) from typing_extensions import Literal from aiohttp import WSMsgType from jinja2 import Template @@ -46,7 +52,7 @@ class LonaServer: - def __init__(self, app, project_root, settings_paths=None, + def __init__(self, project_root, settings_paths=None, settings_pre_overrides=None, settings_post_overrides=None, routes=None): @@ -55,15 +61,7 @@ def __init__(self, app, project_root, settings_paths=None, self._websocket_connections = [] self._loop = None self._worker_pool = None - - server_logger.debug("starting server in '%s'", project_root) - - # setup aiohttp app - self._app = app - self._app['lona_server'] = self - - self._app.on_startup.append(self._start) - self._app.on_shutdown.append(self._stop) + self._app: Application = None # setup settings server_logger.debug('setup settings') @@ -96,6 +94,17 @@ def __init__(self, app, project_root, settings_paths=None, self.settings.update(settings_post_overrides) + # setup aiohttp app + server_logger.debug("starting server in '%s'", project_root) + + self._app = Application( + client_max_size=self.settings.AIOHTTP_CLIENT_MAX_SIZE, + ) + self._app['lona_server'] = self + + self._app.on_startup.append(self._start) + self._app.on_shutdown.append(self._stop) + # setup templating server_logger.debug('setup templating')