Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
046fdcb
first poc
saviorand Feb 27, 2026
3c1acc9
remove pixi.lock
saviorand Feb 27, 2026
74e72d7
fix register passable and pull small-time from git
saviorand Feb 28, 2026
269ac38
import from ffi not sys ffi
saviorand Feb 28, 2026
12495c0
refactor error variants
saviorand Feb 28, 2026
c7df784
Merge pull request #287 from Lightbug-HQ/fix/typed-errors-variant-alias
saviorand Feb 28, 2026
f0cc633
fix int conversions
saviorand Feb 28, 2026
0219b33
replace write_bytes with write_string
saviorand Mar 1, 2026
b435285
fix remaining server compilation errors
saviorand Mar 1, 2026
4c32fef
Merge branch 'main' into feature/json-support
saviorand Mar 1, 2026
566b291
update pixi.lock
saviorand Mar 1, 2026
2c08fb9
fix parameter warnings
saviorand Mar 1, 2026
7aad640
remove bytes.memmove
saviorand Mar 1, 2026
0a1dfbe
relax JsonOK constraint
saviorand Mar 1, 2026
1c4e839
wip refactoring
saviorand Mar 16, 2026
bd28fc6
wip moving to 26.2
saviorand Mar 27, 2026
2c5bcbc
wip fixing compilation errors
saviorand Mar 28, 2026
53dcb19
get the example to compile
saviorand Mar 28, 2026
dcc813a
fix warnings
saviorand Mar 28, 2026
8e52d13
remove binary
saviorand Mar 28, 2026
33093cb
fix compilation in tests
saviorand Mar 28, 2026
3559838
use published deps
saviorand Mar 28, 2026
787d974
dont re-export json struct
saviorand Mar 28, 2026
5dc13e8
fix package
saviorand Mar 28, 2026
e9e8bbf
restore socketclosed
saviorand Mar 29, 2026
5d3e401
add json example server
saviorand Mar 29, 2026
c7c9052
fix integration tests
saviorand Mar 29, 2026
ae894fc
update versions
saviorand Mar 29, 2026
5e67541
fix more errors
saviorand Mar 29, 2026
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ __pycache__
.mcp.json
.claude
CLAUDE.md
.kli/**
*.xml
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Once you have a Mojo project set up locally,

```toml
[dependencies]
lightbug_http = ">=0.26.1.2,<0.26.2"
lightbug_http = ">=0.26.2.0,<0.26.3"
```

3. Run `pixi install` at the root of your project, where `pixi.toml` is located
Expand Down Expand Up @@ -151,6 +151,33 @@ struct ExampleRouter(HTTPService):

We plan to add more advanced routing functionality in a future library called `lightbug_api`, see [Roadmap](#roadmap) for more details.

### JSON

Use `json_decode[T]` to deserialize a request body into a typed struct, and `Json(value)` to return a JSON response:

```mojo
from lightbug_http import OK, HTTPRequest, HTTPResponse, HTTPService
from lightbug_http.http.json import Json, json_decode

@fieldwise_init
struct GreetRequest(Movable, Defaultable):
var name: String
fn __init__(out self): self.name = ""

@fieldwise_init
struct GreetResponse(Movable, Defaultable):
var message: String
fn __init__(out self): self.message = ""

@fieldwise_init
struct JsonService(HTTPService):
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
var body = json_decode[GreetRequest](req)
return OK(Json(GreetResponse(String("Hello, ", body.name, "!"))))
```

JSON support is powered by [emberjson](https://github.com/bgreni/EmberJson).


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

Expand Down Expand Up @@ -190,7 +217,7 @@ Check out the examples directory for more example services built with Lightbug,

We're working on support for the following (contributors welcome!):

- [ ] [JSON support](https://github.com/saviorand/lightbug_http/issues/4)
- [x] [JSON support](https://github.com/saviorand/lightbug_http/issues/4)
- [ ] Complete HTTP/1.x support compliant with RFC 9110/9112 specs (see issues)
- [ ] [SSL/HTTPS support](https://github.com/saviorand/lightbug_http/issues/20)
- [ ] [Multiple simultaneous connections](https://github.com/saviorand/lightbug_http/issues/5), [parallelization and performance optimizations](https://github.com/saviorand/lightbug_http/issues/6)
Expand Down
4 changes: 2 additions & 2 deletions examples/echo_server/pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ server = "mojo run server.mojo"
client = "mojo run client.mojo"

[dependencies]
mojo = ">=0.26.1.0,<0.26.2.0"
mojo = ">=0.26.2.0,<0.26.3"
curl_wrapper = { git = "https://github.com/thatstoasty/mojo-curl.git", branch = "main", subdirectory = "shim" }
floki = ">=0.1.0,<0.2"
lightbug_http = ">=0.26.1.2,<0.26.2.0"
lightbug_http = ">=0.26.2.0,<0.26.3"
18 changes: 18 additions & 0 deletions examples/json_service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# JSON Service

A simple JSON API example using `lightbug_http`. The server accepts a POST request with a JSON body and returns a JSON response.

To run the server:

```bash
pixi run server
```

Then send a request:

```bash
curl -X POST http://localhost:8080/greet \
-H "Content-Type: application/json" \
-d '{"name": "Alice"}'
# {"message":"Hello, Alice!"}
```
13 changes: 13 additions & 0 deletions examples/json_service/pixi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[workspace]
channels = ["https://conda.modular.com/max", "https://repo.prefix.dev/modular-community", "https://repo.prefix.dev/mojo-community", "conda-forge"]
name = "json_service"
platforms = ["osx-arm64", "linux-64", "linux-aarch64"]
version = "0.1.0"
preview = ["pixi-build"]

[tasks]
server = "mojo run server.mojo"

[dependencies]
mojo = ">=2.0,<0.26.3"
lightbug_http = ">=0.26.2.0,<0.26.3"
34 changes: 34 additions & 0 deletions examples/json_service/server.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from lightbug_http import OK, NotFound, HTTPRequest, HTTPResponse, HTTPService, Server
from lightbug_http.http.json import Json, json_decode


@fieldwise_init
struct GreetRequest(Movable, Defaultable):
var name: String

fn __init__(out self):
self.name = ""


@fieldwise_init
struct GreetResponse(Movable, Defaultable):
var message: String

fn __init__(out self):
self.message = ""


@fieldwise_init
struct JsonService(HTTPService):
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
if req.uri.path == "/greet":
var body = json_decode[GreetRequest](req)
var response = GreetResponse(String("Hello, ", body.name, "!"))
return OK(Json(response))
return NotFound(req.uri.path)


fn main() raises:
var server = Server()
var handler = JsonService()
server.listen_and_serve("localhost:8080", handler)
13 changes: 13 additions & 0 deletions lightbug.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from lightbug_http import Welcome
from lightbug_http.server import Server
from std.os.env import getenv


fn main() raises:
var server = Server()
var handler = Welcome()

var host = getenv("DEFAULT_SERVER_HOST", "localhost")
var port = getenv("DEFAULT_SERVER_PORT", "8080")

server.listen_and_serve(host + ":" + port, handler)
2 changes: 1 addition & 1 deletion lightbug_http/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ from lightbug_http.service import Counter, HTTPService, Welcome
from lightbug_http.uri import URI

from lightbug_http.cookie import Cookie, RequestCookieJar, ResponseCookieJar
from lightbug_http.http import OK, HTTPRequest, HTTPResponse, NotFound, SeeOther, StatusCode
from lightbug_http.http import OK, HTTPRequest, HTTPResponse, NotFound, SeeOther, StatusCode, Json, json_decode, JsonSerializable, JsonDeserializable
Loading
Loading