Skip to content

Commit a0c01a0

Browse files
Server() interface (#153)
1 parent 0e8a4e0 commit a0c01a0

File tree

8 files changed

+56
-62
lines changed

8 files changed

+56
-62
lines changed

src/ahttpx/_server.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import contextlib
21
import logging
32

43
from ._body import RequestContent
@@ -9,7 +8,7 @@
98
from ._network import NetworkBackend, sleep
109

1110
__all__ = [
12-
"serve_http", "run"
11+
"Server"
1312
]
1413

1514
logger = logging.getLogger("httpx.server")
@@ -84,34 +83,32 @@ async def _send_body(self, response: Response):
8483
await self._parser.send_body(b'')
8584

8685

87-
class HTTPServer:
88-
def __init__(self, host, port):
89-
self.url = f"http://{host}:{port}/"
86+
class Server:
87+
def __init__(self, app):
88+
self.app = app
89+
self.backend = NetworkBackend()
90+
self.url = 'http://127.0.0.1:8080/'
9091

91-
async def wait(self):
92-
while(True):
93-
await sleep(1)
92+
logging.basicConfig(
93+
format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
94+
datefmt="%Y-%m-%d %H:%M:%S",
95+
level=logging.DEBUG
96+
)
9497

98+
async def __aenter__(self):
99+
self._tcp_server = await self.backend.serve("127.0.0.1", 8080, self.handle_stream)
100+
await self._tcp_server.__aenter__()
101+
logger.info(f"Serving on http://127.0.0.1:8080 (Press CTRL+C to quit)")
102+
return self
95103

96-
@contextlib.asynccontextmanager
97-
async def serve_http(endpoint):
98-
async def handler(stream):
99-
connection = HTTPConnection(stream, endpoint)
100-
await connection.handle_requests()
101-
102-
logging.basicConfig(
103-
format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
104-
datefmt="%Y-%m-%d %H:%M:%S",
105-
level=logging.DEBUG
106-
)
107-
108-
backend = NetworkBackend()
109-
async with await backend.serve("127.0.0.1", 8080, handler) as server:
110-
server = HTTPServer(server.host, server.port)
111-
logger.info(f"Serving on {server.url} (Press CTRL+C to quit)")
112-
yield server
104+
async def __aexit__(self, exc_type, exc_val, exc_tb):
105+
await self._tcp_server.__aexit__(exc_type, exc_val, exc_tb)
113106

107+
async def handle_stream(self, stream):
108+
connection = HTTPConnection(stream, self.app)
109+
await connection.handle_requests()
114110

115-
async def run(app):
116-
async with await serve_http(app) as server:
117-
server.wait()
111+
async def serve(self):
112+
async with self as server:
113+
while(True):
114+
await sleep(1)

src/httpx/_server.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import contextlib
21
import logging
32

43
from ._body import RequestContent
@@ -9,7 +8,7 @@
98
from ._network import NetworkBackend, sleep
109

1110
__all__ = [
12-
"serve_http", "run"
11+
"Server"
1312
]
1413

1514
logger = logging.getLogger("httpx.server")
@@ -84,34 +83,32 @@ def _send_body(self, response: Response):
8483
self._parser.send_body(b'')
8584

8685

87-
class HTTPServer:
88-
def __init__(self, host, port):
89-
self.url = f"http://{host}:{port}/"
86+
class Server:
87+
def __init__(self, app):
88+
self.app = app
89+
self.backend = NetworkBackend()
90+
self.url = 'http://127.0.0.1:8080/'
9091

91-
def wait(self):
92-
while(True):
93-
sleep(1)
92+
logging.basicConfig(
93+
format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
94+
datefmt="%Y-%m-%d %H:%M:%S",
95+
level=logging.DEBUG
96+
)
9497

98+
def __enter__(self):
99+
self._tcp_server = self.backend.serve("127.0.0.1", 8080, self.handle_stream)
100+
self._tcp_server.__enter__()
101+
logger.info(f"Serving on http://127.0.0.1:8080 (Press CTRL+C to quit)")
102+
return self
95103

96-
@contextlib.contextmanager
97-
def serve_http(endpoint):
98-
def handler(stream):
99-
connection = HTTPConnection(stream, endpoint)
100-
connection.handle_requests()
101-
102-
logging.basicConfig(
103-
format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
104-
datefmt="%Y-%m-%d %H:%M:%S",
105-
level=logging.DEBUG
106-
)
107-
108-
backend = NetworkBackend()
109-
with backend.serve("127.0.0.1", 8080, handler) as server:
110-
server = HTTPServer(server.host, server.port)
111-
logger.info(f"Serving on {server.url} (Press CTRL+C to quit)")
112-
yield server
104+
def __exit__(self, exc_type, exc_val, exc_tb):
105+
self._tcp_server.__exit__(exc_type, exc_val, exc_tb)
113106

107+
def handle_stream(self, stream):
108+
connection = HTTPConnection(stream, self.app)
109+
connection.handle_requests()
114110

115-
def run(app):
116-
with serve_http(app) as server:
117-
server.wait()
111+
def serve(self):
112+
with self as server:
113+
while(True):
114+
sleep(1)

tests/test_ahttpx/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def client():
2121

2222
@pytest.fixture
2323
async def server():
24-
async with ahttpx.serve_http(echo) as server:
24+
async with ahttpx.Server(echo) as server:
2525
yield server
2626

2727

tests/test_ahttpx/test_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async def hello_world(request):
99

1010
@pytest.fixture
1111
async def server():
12-
async with ahttpx.serve_http(hello_world) as server:
12+
async with ahttpx.Server(hello_world) as server:
1313
yield server
1414

1515

tests/test_ahttpx/test_quickstart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def echo(request):
1515

1616
@pytest.fixture
1717
async def server():
18-
async with ahttpx.serve_http(echo) as server:
18+
async with ahttpx.Server(echo) as server:
1919
yield server
2020

2121

tests/test_httpx/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def client():
2121

2222
@pytest.fixture
2323
def server():
24-
with httpx.serve_http(echo) as server:
24+
with httpx.Server(echo) as server:
2525
yield server
2626

2727

tests/test_httpx/test_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def hello_world(request):
99

1010
@pytest.fixture
1111
def server():
12-
with httpx.serve_http(hello_world) as server:
12+
with httpx.Server(hello_world) as server:
1313
yield server
1414

1515

tests/test_httpx/test_quickstart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def echo(request):
1515

1616
@pytest.fixture
1717
def server():
18-
with httpx.serve_http(echo) as server:
18+
with httpx.Server(echo) as server:
1919
yield server
2020

2121

0 commit comments

Comments
 (0)