Skip to content

Commit b78513c

Browse files
authored
Merge pull request #286 from thatstoasty/examples
Add echo server and client example
2 parents 5a70b41 + 9ebac0c commit b78513c

File tree

12 files changed

+1837
-271
lines changed

12 files changed

+1837
-271
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ Lightbug currently has the following features:
3232
- [x] Pure Mojo HTTP 1.1 server with no Python dependencies. Everything is fully typed, with no `def` functions used
3333
- [x] Cookie support
3434

35-
### Check Out These Mojo Libraries:
35+
### Check Out These Mojo Libraries:
3636

37-
- Logging - [@toasty/stump](https://github.com/thatstoasty/stump)
38-
- CLI and Terminal - [@toasty/prism](https://github.com/thatstoasty/prism), [@toasty/mog](https://github.com/thatstoasty/mog)
39-
- Date/Time - [@mojoto/morrow](https://github.com/mojoto/morrow.mojo) and [@toasty/small_time](https://github.com/thatstoasty/small_time)
37+
- HTTP Client - [@thatstoasty/floki](https://github.com/thatstoasty/floki)
38+
- CLI and Terminal - [@thatstoasty/prism](https://github.com/thatstoasty/prism), [@thatstoasty/mog](https://github.com/thatstoasty/mog)
39+
- Date/Time - [@mojoto/morrow](https://github.com/mojoto/morrow.mojo) and [@thatstoasty/small_time](https://github.com/thatstoasty/small_time)
4040

4141
<p align="right">(<a href="#readme-top">back to top</a>)</p>
4242

@@ -176,6 +176,11 @@ struct Welcome(HTTPService):
176176
return NotFound(req.uri.path)
177177
```
178178

179+
<!-- Examples -->
180+
## Examples
181+
182+
Check out the examples directory for more example services built with Lightbug, including an echo server and client implementation!
183+
179184
<!-- ROADMAP -->
180185
## Roadmap
181186

client.mojo

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/echo_server/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Echo Server
2+
3+
A simple echo server example using `lightbug_http` for the server and `floki` for the client. The server listens for incoming HTTP requests and responds with the same body and content type.
4+
5+
To run the server, execute the following command in the terminal:
6+
7+
```bash
8+
pixi run server
9+
```
10+
11+
In a separate terminal, you can run the client to send a request to the server:
12+
13+
```bash
14+
pixi run client
15+
```

examples/echo_server/client.mojo

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import floki
2+
3+
4+
fn main() raises:
5+
var response = floki.post(
6+
"localhost:8080",
7+
headers={"Content-Type": "text/plain"},
8+
data="Hello, Echo Server!".as_bytes()
9+
)
10+
print(response.body.as_string_slice())
11+
12+
response = floki.post(
13+
"localhost:8080",
14+
headers={"Content-Type": "application/json"},
15+
data={"message": "Hello, Echo Server!"}
16+
)
17+
print(response.body.as_json())

examples/echo_server/pixi.lock

Lines changed: 1680 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/echo_server/pixi.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[workspace]
2+
authors = ["Mikhail Tavarez <miktavarez@gmail.com>"]
3+
channels = ["https://conda.modular.com/max", "https://repo.prefix.dev/modular-community", "https://repo.prefix.dev/mojo-community", "conda-forge"]
4+
name = "echo_server"
5+
platforms = ["osx-arm64", "linux-64", "linux-aarch64"]
6+
version = "0.1.0"
7+
preview = ["pixi-build"]
8+
9+
[tasks]
10+
server = "mojo run server.mojo"
11+
client = "mojo run client.mojo"
12+
13+
[dependencies]
14+
mojo = ">=0.26.1.0,<0.26.2.0"
15+
curl_wrapper = { git = "https://github.com/thatstoasty/mojo-curl.git", branch = "main", subdirectory = "shim" }
16+
floki = ">=0.1.0,<0.2"
17+
lightbug_http = ">=0.26.1.2,<0.26.2.0"

examples/echo_server/server.mojo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from lightbug_http import OK, HTTPRequest, HTTPResponse, HTTPService, Server
2+
3+
4+
@fieldwise_init
5+
struct EchoService(HTTPService):
6+
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
7+
var content_type = req.headers.get("content-type")
8+
return OK(req.body_raw, content_type.or_else("text/plain; charset=utf-8"))
9+
10+
11+
fn main() raises:
12+
var server = Server()
13+
var handler = EchoService()
14+
server.listen_and_serve("localhost:8080", handler)

lightbug_http/address.mojo

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,6 @@ fn is_ipv6(network: NetworkType) -> Bool:
426426
return network in (NetworkType.tcp6, NetworkType.udp6, NetworkType.ip6)
427427

428428

429-
430-
431429
@fieldwise_init
432430
@register_passable("trivial")
433431
struct ParseEmptyAddressError(CustomError):
@@ -548,8 +546,6 @@ struct ParseIPProtocolPortError(CustomError):
548546
return Self.message
549547

550548

551-
552-
553549
@fieldwise_init
554550
@register_passable("trivial")
555551
struct GetaddrinfoNullAddrError(CustomError):
@@ -574,7 +570,6 @@ struct GetaddrinfoError(CustomError):
574570
return Self.message
575571

576572

577-
578573
@fieldwise_init
579574
struct GetIPAddressError(Movable, Stringable, Writable):
580575
"""Typed error variant for get_ip_address() function."""

lightbug_http/connection.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ struct TCPConnection[network: NetworkType = NetworkType.tcp4]:
371371
"""
372372
var total_sent: UInt = 0
373373
while total_sent < UInt(len(buf)):
374-
var sent = self.socket.send(buf[Int(total_sent):])
374+
var sent = self.socket.send(buf[Int(total_sent) :])
375375
total_sent += sent
376376
return total_sent
377377

lightbug_http/header.mojo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ fn encode_latin1_header_value(value: String) -> List[UInt8]:
367367
var b4 = utf8[i + 3]
368368
if b2 >= 0x80 and b2 <= 0xBF and b3 >= 0x80 and b3 <= 0xBF and b4 >= 0x80 and b4 <= 0xBF:
369369
seq_len = 4
370-
codepoint = ((Int(b) & 0x07) << 18) | ((Int(b2) & 0x3F) << 12) | ((Int(b3) & 0x3F) << 6) | (Int(b4) & 0x3F)
370+
codepoint = (
371+
((Int(b) & 0x07) << 18) | ((Int(b2) & 0x3F) << 12) | ((Int(b3) & 0x3F) << 6) | (Int(b4) & 0x3F)
372+
)
371373

372374
if seq_len > 0 and codepoint <= 0xFF:
373375
out.append(UInt8(codepoint))

0 commit comments

Comments
 (0)