diff --git a/lightbug_http/sys/net.mojo b/lightbug_http/sys/net.mojo index 8bd6aeae..bdd73d51 100644 --- a/lightbug_http/sys/net.mojo +++ b/lightbug_http/sys/net.mojo @@ -81,6 +81,10 @@ fn getaddrinfo[ @value struct SysListener: + """ + A TCP listener that listens for incoming connections and can accept them. + """ + var fd: c_int var __addr: TCPAddr @@ -267,6 +271,16 @@ struct addrinfo_macos(AnAddrInfo): ) fn get_ip_address(self, host: String) raises -> in_addr: + """ + Returns an IP address based on the host. + This is a MacOS-specific implementation. + + Args: + host: String - The host to get the IP from. + + Returns: + UInt32 - The IP address. + """ var host_ptr = to_char_ptr(host) var servinfo = Pointer[Self]().alloc(1) servinfo.store(Self()) @@ -323,6 +337,16 @@ struct addrinfo_unix(AnAddrInfo): ) fn get_ip_address(self, host: String) raises -> in_addr: + """ + Returns an IP address based on the host. + This is a Unix-specific implementation. + + Args: + host: String - The host to get IP from. + + Returns: + UInt32 - The IP address. + """ var host_ptr = to_char_ptr(host) var servinfo = Pointer[Self]().alloc(1) servinfo.store(Self()) @@ -358,7 +382,8 @@ struct addrinfo_unix(AnAddrInfo): fn create_connection(sock: c_int, host: String, port: UInt16) raises -> SysConnection: - """Connect to a server using a socket. + """ + Connect to a server using a socket. Args: sock: Int32 - The socket file descriptor. diff --git a/lightbug_http/sys/server.mojo b/lightbug_http/sys/server.mojo index 4b562976..d0a08137 100644 --- a/lightbug_http/sys/server.mojo +++ b/lightbug_http/sys/server.mojo @@ -12,6 +12,10 @@ from lightbug_http.strings import next_line, NetworkType struct SysServer: + """ + A Mojo-based server that accept incoming requests and delivers HTTP services. + """ + var error_handler: ErrorHandler var name: String @@ -42,6 +46,13 @@ struct SysServer: self.ln = SysListener() fn get_concurrency(self) -> Int: + """ + Retrieve the concurrency level which is either + the configured max_concurrent_connections or the DefaultConcurrency. + + Returns: + Int: concurrency level for the server. + """ var concurrency = self.max_concurrent_connections if concurrency <= 0: concurrency = DefaultConcurrency @@ -50,11 +61,28 @@ struct SysServer: fn listen_and_serve[ T: HTTPService ](inout self, address: String, handler: T) raises -> None: + """ + Listen for incoming connections and serve HTTP requests. + + Args: + address : String - The address (host:port) to listen on. + handler : HTTPService - An object that handles incoming HTTP requests. + """ var __net = SysNet() var listener = __net.listen(NetworkType.tcp4.value, address) self.serve(listener, handler) fn serve[T: HTTPService](inout self, ln: SysListener, handler: T) raises -> None: + """ + Serve HTTP requests. + + Args: + ln : SysListener - TCP server that listens for incoming connections. + handler : HTTPService - An object that handles incoming HTTP requests. + + Raises: + If there is an error while serving requests. + """ # var max_worker_count = self.get_concurrency() # TODO: logic for non-blocking read and write here, see for example https://github.com/valyala/fasthttp/blob/9ba16466dfd5d83e2e6a005576ee0d8e127457e2/server.go#L1789