Skip to content

Commit 77bc821

Browse files
committed
Moved root_path from start and server_forever methods to constructor
1 parent fee4570 commit 77bc821

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

adafruit_httpserver/server.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@
2626
class HTTPServer:
2727
"""A basic socket-based HTTP server."""
2828

29-
def __init__(self, socket_source: Protocol) -> None:
29+
def __init__(self, socket_source: Protocol, root_path: str) -> None:
3030
"""Create a server, and get it ready to run.
3131
3232
:param socket: An object that is a source of sockets. This could be a `socketpool`
3333
in CircuitPython or the `socket` module in CPython.
34+
:param str root_path: Root directory to serve files from
3435
"""
3536
self._buffer = bytearray(1024)
3637
self._timeout = 1
3738
self.routes = _HTTPRoutes()
3839
self._socket_source = socket_source
3940
self._sock = None
40-
self.root_path = "/"
41+
self.root_path = root_path
4142

4243
def route(self, path: str, method: HTTPMethod = HTTPMethod.GET) -> Callable:
4344
"""
@@ -63,32 +64,28 @@ def route_decorator(func: Callable) -> Callable:
6364

6465
return route_decorator
6566

66-
def serve_forever(self, host: str, port: int = 80, root_path: str = "") -> None:
67+
def serve_forever(self, host: str, port: int = 80) -> None:
6768
"""Wait for HTTP requests at the given host and port. Does not return.
6869
6970
:param str host: host name or IP address
7071
:param int port: port
71-
:param str root_path: root directory to serve files from
7272
"""
73-
self.start(host, port, root_path)
73+
self.start(host, port)
7474

7575
while True:
7676
try:
7777
self.poll()
7878
except OSError:
7979
continue
8080

81-
def start(self, host: str, port: int = 80, root_path: str = "") -> None:
81+
def start(self, host: str, port: int = 80) -> None:
8282
"""
8383
Start the HTTP server at the given host and port. Requires calling
8484
poll() in a while loop to handle incoming requests.
8585
8686
:param str host: host name or IP address
8787
:param int port: port
88-
:param str root_path: root directory to serve files from
8988
"""
90-
self.root_path = root_path
91-
9289
self._sock = self._socket_source.socket(
9390
self._socket_source.AF_INET, self._socket_source.SOCK_STREAM
9491
)

0 commit comments

Comments
 (0)