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
27 changes: 26 additions & 1 deletion lightbug_http/sys/net.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions lightbug_http/sys/server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down