Skip to content

Commit a0794da

Browse files
committed
add usage examples
1 parent 9e30863 commit a0794da

File tree

5 files changed

+66
-72
lines changed

5 files changed

+66
-72
lines changed

README.md

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
<!-- PROJECT LOGO -->
55
<br />
66
<div align="center">
7-
<a href="https://github.com/othneildrew/Best-README-Template">
87
<img src="static/logo.png" alt="Logo" width="250" height="250">
9-
</a>
108

119
<h3 align="center">Lightbug</h3>
1210

@@ -31,9 +29,12 @@ It currently has the following features:
3129
- [x] Assign your own custom handler to a route
3230

3331

34-
We're working on support for:
35-
- [ ] Better error handling (contributors welcome!)
32+
We're working on support for the following (contributors welcome!):
33+
- [ ] Pure Mojo networking (while most of the code is in Mojo, we call Python's `socket` library in several parts of the code)
34+
- [ ] More request body types (currently only `text/plain`)
35+
- [ ] Better error handling
3636
- [ ] Multiple simultaneous connections and parallelization
37+
- [ ] WebSockets, HTTP 2.0
3738

3839

3940
The plan is to get to a feature set similar to Python frameworks like [Starlette](https://github.com/encode/starlette), but with better performance.
@@ -46,33 +47,46 @@ The plan is to get to a feature set similar to Python frameworks like [Starlette
4647
<!-- GETTING STARTED -->
4748
## Getting Started
4849

49-
The only hard dependency is Mojo!
50+
The only hard dependency for `lightbug_http` is Mojo.
51+
Learn how to set it up on the [Modular website](https://www.modular.com/max/mojo).
52+
53+
Once you have Mojo up and running on your local machine,
5054

5155
1. Clone the repo
5256
```sh
5357
git clone https://github.com/saviorand/mojo-web
5458
```
55-
2. Add your handler in `main.mojo` by passing a struct satisfying the following trait:
59+
2. Add your handler in `main.mojo` by passing a struct that satisfies the following trait:
5660
```mojo
5761
trait HTTPService:
5862
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
5963
...
6064
```
61-
e.g. to make a service that simply prints the request to console:
65+
For example, to make a `Printer` service that simply prints the request to console:
6266
```mojo
6367
@value
6468
struct Printer(HTTPService):
6569
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
66-
let req_body = req.body_raw
67-
print(String(req_body))
68-
return HTTPResponse(
69-
ResponseHeader(
70-
200, String("OK")._buffer, String("Content-Type: text/plain")._buffer
71-
),
72-
req_body,
73-
)
74-
```
70+
let body = req.body_raw
71+
72+
print(String(body))
73+
74+
return OK(body)
75+
```
76+
3. Run `mojo main.mojo`. This will start up a server listening on `localhost:8080`. Or, if you prefer to import the server into your own app:
77+
```mojo
78+
from lightbug_http.io.bytes import Bytes
79+
from lightbug_http.python.server import PythonServer
80+
from lightbug_http.service import Printer
81+
82+
83+
fn main() raises:
84+
var server = PythonServer()
85+
let handler = Printer()
86+
server.listen_and_serve("0.0.0.0:8080", handler)
87+
```
7588

89+
Feel free to change the settings in `listen_and_serve()` to serve on a particular host and port.
7690

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

@@ -81,12 +95,19 @@ The only hard dependency is Mojo!
8195
<!-- ROADMAP -->
8296
## Roadmap
8397

84-
- Mojo-http (this repo)
85-
- [ ] TODOs
86-
- Mojo-api
87-
- Experience of Django, Speed of Rust
88-
- Mojo-web
89-
- Ditch NextJS, Mojo full-stack framework
98+
<div align="center">
99+
<img src="static/roadmap.png" alt="Logo" width="695" height="226">
100+
</div>
101+
102+
Our vision is to develop three libraries, with `lightbug_http` (this repo) as a starting point:
103+
- `lightbug_http` - HTTP infrastructure and basic API development
104+
- `lightbug_api` - Tools to make great APIs fast, with support for OpenAPI spec and domain driven design
105+
- `lightbug_web` - Full-stack web framework for Mojo, similar to NextJS or SvelteKit
106+
107+
The idea is to get to a point where the entire codebase of a simple modern web application can be written in Mojo.
108+
109+
We don't make any promises, though -- this is just a vision, and whether we get there or not depends on many factors, including the support of the community.
110+
90111

91112
See the [open issues](https://github.com/othneildrew/Best-README-Template/issues) for a full list of proposed features (and known issues).
92113

@@ -97,7 +118,7 @@ See the [open issues](https://github.com/othneildrew/Best-README-Template/issues
97118
<!-- CONTRIBUTING -->
98119
## Contributing
99120

100-
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
121+
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. See [CONTRIBUTING.md](./CONTRIBUTING.md) for more details on how to contribute.
101122

102123
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
103124
Don't forget to give the project a star! Thanks again!
@@ -148,7 +169,7 @@ Use this space to list resources you find helpful and would like to give credit
148169

149170
<!-- MARKDOWN LINKS & IMAGES -->
150171
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
151-
[language-shield]: https://img.shields.io/badge/language-mojo-red
172+
[language-shield]: https://img.shields.io/badge/language-mojo-orange
152173
[license-shield]: https://img.shields.io/github/license/saviorand/lightbug_http?logo=github
153174
[license-url]: https://github.com/saviorand/lightbug_http/blob/main/LICENSE
154175
[contributors-shield]: https://img.shields.io/badge/contributors-welcome!-blue

lightbug_http/http.mojo

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ struct HTTPResponse(Response):
241241
return self.header.connection_close()
242242

243243

244+
fn OK(body: Bytes) -> HTTPResponse:
245+
return HTTPResponse(
246+
ResponseHeader(
247+
200, String("OK")._buffer, String("Content-Type: text/plain")._buffer
248+
),
249+
body,
250+
)
251+
252+
244253
fn encode(res: HTTPResponse) -> Bytes:
245254
var res_str = String()
246255
let protocol = strHttp11
@@ -258,33 +267,3 @@ fn encode(res: HTTPResponse) -> Bytes:
258267
res_str += String("\r\n")
259268
res_str += res.body_raw
260269
return res_str._buffer
261-
262-
263-
# Rust example
264-
265-
# pub fn encode(mut rsp: Response, buf: &mut BytesMut) {
266-
# if rsp.status_message.code == 200 {
267-
# buf.extend_from_slice(b"HTTP/1.1 200 Ok\r\nServer: M\r\nDate: ");
268-
# } else {
269-
# buf.extend_from_slice(b"HTTP/1.1 ");
270-
# let mut code = itoa::Buffer::new();
271-
# buf.extend_from_slice(code.format(rsp.status_message.code).as_bytes());
272-
# buf.extend_from_slice(b" ");
273-
# buf.extend_from_slice(rsp.status_message.msg.as_bytes());
274-
# buf.extend_from_slice(b"\r\nServer: M\r\nDate: ");
275-
# }
276-
# crate::date::append_date(buf);
277-
# buf.extend_from_slice(b"\r\nContent-Length: ");
278-
# let mut length = itoa::Buffer::new();
279-
# buf.extend_from_slice(length.format(rsp.body_len()).as_bytes());
280-
281-
# // SAFETY: we already have bound check when insert headers
282-
# let headers = unsafe { rsp.headers.get_unchecked(..rsp.headers_len) };
283-
# for h in headers {
284-
# buf.extend_from_slice(b"\r\n");
285-
# buf.extend_from_slice(h.as_bytes());
286-
# }
287-
288-
# buf.extend_from_slice(b"\r\n\r\n");
289-
# buf.extend_from_slice(rsp.get_body());
290-
# }

lightbug_http/service.mojo

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
from lightbug_http.http import HTTPRequest, HTTPResponse
1+
from lightbug_http.http import HTTPRequest, HTTPResponse, OK
22
from lightbug_http.io.bytes import Bytes
33

44

55
trait HTTPService:
66
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
77
...
8+
9+
10+
@value
11+
struct Printer(HTTPService):
12+
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
13+
let body = req.body_raw
14+
15+
print(String(body))
16+
17+
return OK(body)

main.mojo

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
from lightbug_http.io.bytes import Bytes
22
from lightbug_http.python.server import PythonServer
3-
from lightbug_http.client import Client
4-
from lightbug_http.header import ResponseHeader
5-
from lightbug_http.http import HTTPRequest, HTTPResponse
6-
from lightbug_http.service import HTTPService
7-
8-
9-
@value
10-
struct Printer(HTTPService):
11-
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
12-
let req_body = req.body_raw
13-
print(String(req_body))
14-
return HTTPResponse(
15-
ResponseHeader(
16-
200, String("OK")._buffer, String("Content-Type: text/plain")._buffer
17-
),
18-
req_body,
19-
)
3+
from lightbug_http.service import Printer
204

215

226
fn main() raises:

static/roadmap.png

46.8 KB
Loading

0 commit comments

Comments
 (0)