Skip to content

Commit 92d0748

Browse files
authored
Merge pull request #30 from SchmittB/feature/add-documentation
Add documentation
2 parents 060fa52 + bb6ad9e commit 92d0748

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lightbug_http/sys/net.mojo

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ fn getaddrinfo[
8181

8282
@value
8383
struct SysListener:
84+
"""
85+
A TCP listener that listens for incoming connections and can accept them.
86+
"""
87+
8488
var fd: c_int
8589
var __addr: TCPAddr
8690

@@ -267,6 +271,16 @@ struct addrinfo_macos(AnAddrInfo):
267271
)
268272

269273
fn get_ip_address(self, host: String) raises -> in_addr:
274+
"""
275+
Returns an IP address based on the host.
276+
This is a MacOS-specific implementation.
277+
278+
Args:
279+
host: String - The host to get the IP from.
280+
281+
Returns:
282+
UInt32 - The IP address.
283+
"""
270284
var host_ptr = to_char_ptr(host)
271285
var servinfo = Pointer[Self]().alloc(1)
272286
servinfo.store(Self())
@@ -323,6 +337,16 @@ struct addrinfo_unix(AnAddrInfo):
323337
)
324338

325339
fn get_ip_address(self, host: String) raises -> in_addr:
340+
"""
341+
Returns an IP address based on the host.
342+
This is a Unix-specific implementation.
343+
344+
Args:
345+
host: String - The host to get IP from.
346+
347+
Returns:
348+
UInt32 - The IP address.
349+
"""
326350
var host_ptr = to_char_ptr(host)
327351
var servinfo = Pointer[Self]().alloc(1)
328352
servinfo.store(Self())
@@ -358,7 +382,8 @@ struct addrinfo_unix(AnAddrInfo):
358382

359383

360384
fn create_connection(sock: c_int, host: String, port: UInt16) raises -> SysConnection:
361-
"""Connect to a server using a socket.
385+
"""
386+
Connect to a server using a socket.
362387
363388
Args:
364389
sock: Int32 - The socket file descriptor.

lightbug_http/sys/server.mojo

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ from lightbug_http.strings import next_line, NetworkType
1212

1313

1414
struct SysServer:
15+
"""
16+
A Mojo-based server that accept incoming requests and delivers HTTP services.
17+
"""
18+
1519
var error_handler: ErrorHandler
1620

1721
var name: String
@@ -42,6 +46,13 @@ struct SysServer:
4246
self.ln = SysListener()
4347

4448
fn get_concurrency(self) -> Int:
49+
"""
50+
Retrieve the concurrency level which is either
51+
the configured max_concurrent_connections or the DefaultConcurrency.
52+
53+
Returns:
54+
Int: concurrency level for the server.
55+
"""
4556
var concurrency = self.max_concurrent_connections
4657
if concurrency <= 0:
4758
concurrency = DefaultConcurrency
@@ -50,11 +61,28 @@ struct SysServer:
5061
fn listen_and_serve[
5162
T: HTTPService
5263
](inout self, address: String, handler: T) raises -> None:
64+
"""
65+
Listen for incoming connections and serve HTTP requests.
66+
67+
Args:
68+
address : String - The address (host:port) to listen on.
69+
handler : HTTPService - An object that handles incoming HTTP requests.
70+
"""
5371
var __net = SysNet()
5472
var listener = __net.listen(NetworkType.tcp4.value, address)
5573
self.serve(listener, handler)
5674

5775
fn serve[T: HTTPService](inout self, ln: SysListener, handler: T) raises -> None:
76+
"""
77+
Serve HTTP requests.
78+
79+
Args:
80+
ln : SysListener - TCP server that listens for incoming connections.
81+
handler : HTTPService - An object that handles incoming HTTP requests.
82+
83+
Raises:
84+
If there is an error while serving requests.
85+
"""
5886
# var max_worker_count = self.get_concurrency()
5987
# TODO: logic for non-blocking read and write here, see for example https://github.com/valyala/fasthttp/blob/9ba16466dfd5d83e2e6a005576ee0d8e127457e2/server.go#L1789
6088

0 commit comments

Comments
 (0)