Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions src/ahttpx/_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib
import logging

from ._body import RequestContent
Expand All @@ -9,7 +8,7 @@
from ._network import NetworkBackend, sleep

__all__ = [
"serve_http", "run"
"Server"
]

logger = logging.getLogger("httpx.server")
Expand Down Expand Up @@ -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)
53 changes: 25 additions & 28 deletions src/httpx/_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib
import logging

from ._body import RequestContent
Expand All @@ -9,7 +8,7 @@
from ._network import NetworkBackend, sleep

__all__ = [
"serve_http", "run"
"Server"
]

logger = logging.getLogger("httpx.server")
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/test_ahttpx/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/test_ahttpx/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/test_ahttpx/test_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/test_httpx/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/test_httpx/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/test_httpx/test_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading