diff --git a/src/ahttpx/_server.py b/src/ahttpx/_server.py index cae3e3a..d4c8787 100644 --- a/src/ahttpx/_server.py +++ b/src/ahttpx/_server.py @@ -1,4 +1,3 @@ -import contextlib import logging from ._body import RequestContent @@ -9,7 +8,7 @@ from ._network import NetworkBackend, sleep __all__ = [ - "serve_http", "run" + "Server" ] logger = logging.getLogger("httpx.server") @@ -84,34 +83,32 @@ async def _send_body(self, response: Response): await self._parser.send_body(b'') -class HTTPServer: - def __init__(self, host, port): - self.url = f"http://{host}:{port}/" +class Server: + def __init__(self, app): + self.app = app + self.backend = NetworkBackend() + self.url = 'http://127.0.0.1:8080/' - async def wait(self): - while(True): - await sleep(1) + logging.basicConfig( + format="%(levelname)s [%(asctime)s] %(name)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + level=logging.DEBUG + ) + async def __aenter__(self): + self._tcp_server = await self.backend.serve("127.0.0.1", 8080, self.handle_stream) + await self._tcp_server.__aenter__() + logger.info(f"Serving on http://127.0.0.1:8080 (Press CTRL+C to quit)") + return self -@contextlib.asynccontextmanager -async def serve_http(endpoint): - async def handler(stream): - connection = HTTPConnection(stream, endpoint) - await connection.handle_requests() - - logging.basicConfig( - format="%(levelname)s [%(asctime)s] %(name)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - level=logging.DEBUG - ) - - backend = NetworkBackend() - async with await backend.serve("127.0.0.1", 8080, handler) as server: - server = HTTPServer(server.host, server.port) - logger.info(f"Serving on {server.url} (Press CTRL+C to quit)") - yield server + async def __aexit__(self, exc_type, exc_val, exc_tb): + await self._tcp_server.__aexit__(exc_type, exc_val, exc_tb) + async def handle_stream(self, stream): + connection = HTTPConnection(stream, self.app) + await connection.handle_requests() -async def run(app): - async with await serve_http(app) as server: - server.wait() + async def serve(self): + async with self as server: + while(True): + await sleep(1) diff --git a/src/httpx/_server.py b/src/httpx/_server.py index ef064f1..1161cf4 100644 --- a/src/httpx/_server.py +++ b/src/httpx/_server.py @@ -1,4 +1,3 @@ -import contextlib import logging from ._body import RequestContent @@ -9,7 +8,7 @@ from ._network import NetworkBackend, sleep __all__ = [ - "serve_http", "run" + "Server" ] logger = logging.getLogger("httpx.server") @@ -84,34 +83,32 @@ def _send_body(self, response: Response): self._parser.send_body(b'') -class HTTPServer: - def __init__(self, host, port): - self.url = f"http://{host}:{port}/" +class Server: + def __init__(self, app): + self.app = app + self.backend = NetworkBackend() + self.url = 'http://127.0.0.1:8080/' - def wait(self): - while(True): - sleep(1) + logging.basicConfig( + format="%(levelname)s [%(asctime)s] %(name)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + level=logging.DEBUG + ) + def __enter__(self): + self._tcp_server = self.backend.serve("127.0.0.1", 8080, self.handle_stream) + self._tcp_server.__enter__() + logger.info(f"Serving on http://127.0.0.1:8080 (Press CTRL+C to quit)") + return self -@contextlib.contextmanager -def serve_http(endpoint): - def handler(stream): - connection = HTTPConnection(stream, endpoint) - connection.handle_requests() - - logging.basicConfig( - format="%(levelname)s [%(asctime)s] %(name)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - level=logging.DEBUG - ) - - backend = NetworkBackend() - with backend.serve("127.0.0.1", 8080, handler) as server: - server = HTTPServer(server.host, server.port) - logger.info(f"Serving on {server.url} (Press CTRL+C to quit)") - yield server + def __exit__(self, exc_type, exc_val, exc_tb): + self._tcp_server.__exit__(exc_type, exc_val, exc_tb) + def handle_stream(self, stream): + connection = HTTPConnection(stream, self.app) + connection.handle_requests() -def run(app): - with serve_http(app) as server: - server.wait() + def serve(self): + with self as server: + while(True): + sleep(1) diff --git a/tests/test_ahttpx/test_client.py b/tests/test_ahttpx/test_client.py index f48133f..d203834 100644 --- a/tests/test_ahttpx/test_client.py +++ b/tests/test_ahttpx/test_client.py @@ -21,7 +21,7 @@ async def client(): @pytest.fixture async def server(): - async with ahttpx.serve_http(echo) as server: + async with ahttpx.Server(echo) as server: yield server diff --git a/tests/test_ahttpx/test_pool.py b/tests/test_ahttpx/test_pool.py index 072da67..77cb57c 100644 --- a/tests/test_ahttpx/test_pool.py +++ b/tests/test_ahttpx/test_pool.py @@ -9,7 +9,7 @@ async def hello_world(request): @pytest.fixture async def server(): - async with ahttpx.serve_http(hello_world) as server: + async with ahttpx.Server(hello_world) as server: yield server diff --git a/tests/test_ahttpx/test_quickstart.py b/tests/test_ahttpx/test_quickstart.py index c16a92e..96d7a40 100644 --- a/tests/test_ahttpx/test_quickstart.py +++ b/tests/test_ahttpx/test_quickstart.py @@ -15,7 +15,7 @@ async def echo(request): @pytest.fixture async def server(): - async with ahttpx.serve_http(echo) as server: + async with ahttpx.Server(echo) as server: yield server diff --git a/tests/test_httpx/test_client.py b/tests/test_httpx/test_client.py index b9c261b..f0ee3b8 100644 --- a/tests/test_httpx/test_client.py +++ b/tests/test_httpx/test_client.py @@ -21,7 +21,7 @@ def client(): @pytest.fixture def server(): - with httpx.serve_http(echo) as server: + with httpx.Server(echo) as server: yield server diff --git a/tests/test_httpx/test_pool.py b/tests/test_httpx/test_pool.py index 04cd024..27615f1 100644 --- a/tests/test_httpx/test_pool.py +++ b/tests/test_httpx/test_pool.py @@ -9,7 +9,7 @@ def hello_world(request): @pytest.fixture def server(): - with httpx.serve_http(hello_world) as server: + with httpx.Server(hello_world) as server: yield server diff --git a/tests/test_httpx/test_quickstart.py b/tests/test_httpx/test_quickstart.py index 8686beb..1db37a4 100644 --- a/tests/test_httpx/test_quickstart.py +++ b/tests/test_httpx/test_quickstart.py @@ -15,7 +15,7 @@ def echo(request): @pytest.fixture def server(): - with httpx.serve_http(echo) as server: + with httpx.Server(echo) as server: yield server