Skip to content

Commit 77acf81

Browse files
committed
add basic imports to init
1 parent 56313cf commit 77acf81

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

lightbug_http/__init__.mojo

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from lightbug_http.http import HTTPRequest, HTTPResponse, OK
2+
from lightbug_http.service import HTTPService, Printer
3+
from lightbug_http.sys.server import SysServer
4+
5+
16
trait DefaultConstructible:
27
fn __init__(inout self):
38
...

lightbug_http/sys/net.mojo

+10-25
Original file line numberDiff line numberDiff line change
@@ -332,31 +332,23 @@ struct SysListenConfig(ListenConfig):
332332
let conv_status = inet_pton(address_family, to_char_ptr(addr.ip), ip_buf)
333333
let raw_ip = ip_buf.bitcast[c_uint]().load()
334334

335-
print("inet_pton: " + raw_ip.__str__() + " :: status: " + conv_status.__str__())
336-
337335
let bin_port = htons(UInt16(addr.port))
338-
print("htons: " + "\n" + bin_port.__str__())
339336

340337
var ai = sockaddr_in(address_family, bin_port, raw_ip, StaticTuple[8, c_char]())
341338
let ai_ptr = Pointer[sockaddr_in].address_of(ai).bitcast[sockaddr]()
342339

343340
let sockfd = socket(address_family, SOCK_STREAM, 0)
344341
if sockfd == -1:
345342
print("Socket creation error")
346-
print("sockfd: " + "\n" + sockfd.__str__())
347343

348344
var yes: Int = 1
349-
if (
350-
setsockopt(
351-
sockfd,
352-
SOL_SOCKET,
353-
SO_REUSEADDR,
354-
Pointer[Int].address_of(yes).bitcast[c_void](),
355-
sizeof[Int](),
356-
)
357-
== -1
358-
):
359-
print("set socket options failed")
345+
_ = setsockopt(
346+
sockfd,
347+
SOL_SOCKET,
348+
SO_REUSEADDR,
349+
Pointer[Int].address_of(yes).bitcast[c_void](),
350+
sizeof[Int](),
351+
)
360352

361353
if bind(sockfd, ai_ptr, sizeof[sockaddr_in]()) == -1:
362354
# close(sockfd)
@@ -368,15 +360,8 @@ struct SysListenConfig(ListenConfig):
368360

369361
let listener = SysListener(addr, sockfd)
370362

371-
print(
372-
"Listening on "
373-
+ addr.ip
374-
+ ":"
375-
+ addr.port.__str__()
376-
+ " on sockfd "
377-
+ sockfd.__str__()
378-
+ "Waiting for connections..."
379-
)
363+
print("🔥🐝 Lightbug is listening on " + addr.ip + ":" + addr.port.__str__())
364+
print("Ready to accept connections...")
380365

381366
return listener
382367

@@ -403,7 +388,7 @@ struct SysConnection(Connection):
403388
self.fd = fd
404389

405390
fn read(self, inout buf: Bytes) raises -> Int:
406-
var new_buf = Pointer[UInt8]().alloc(default_buffer_size)
391+
let new_buf = Pointer[UInt8]().alloc(default_buffer_size)
407392
let bytes_recv = recv(self.fd, new_buf, default_buffer_size, 0)
408393
if bytes_recv == -1:
409394
print("Failed to receive message")

main.mojo

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from lightbug_http.sys.server import SysServer
2-
from lightbug_http.service import Printer
1+
from lightbug_http import *
32

43

54
fn main() raises:

0 commit comments

Comments
 (0)