Skip to content

Commit c8b84bf

Browse files
committed
update to 0.26.2
1 parent 1c67ca5 commit c8b84bf

12 files changed

Lines changed: 1229 additions & 6969 deletions

File tree

.mojoenv.example

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Once you have a Mojo project set up locally,
5454
lightbug_api = ">=0.1.0.dev2024092905"
5555
lightbug_http = ">=0.1.4"
5656
```
57-
3. Run `magic install` at the root of your project, where `mojoproject.toml` is located
57+
3. Run `pixi install` at the root of your project, where `pixi.toml` is located
5858
4. Lightbug API should now be installed. You can import all the default imports at once, e.g:
5959
```mojo
6060
from lightbug_api import *

lightbug.mojo

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from lightbug_api import (
2+
App,
3+
BaseRequest,
4+
FromReq,
5+
Router,
6+
HandlerResponse,
7+
JSONType,
8+
)
9+
from lightbug_http import HTTPRequest, HTTPResponse, OK
10+
11+
12+
fn printer(req: HTTPRequest) raises -> HandlerResponse:
13+
print("Got a request on ", req.uri.path, " with method ", req.method)
14+
return OK(req.body_raw)
15+
16+
fn hello(req: HTTPRequest) raises -> HandlerResponse:
17+
return OK("Hello 🔥!")
18+
19+
20+
fn nested(req: HTTPRequest) raises -> HandlerResponse:
21+
print("Handling route:", req.uri.path)
22+
# Returning a string will get marshaled to a proper `OK` response
23+
return req.uri.path
24+
25+
26+
struct Payload(FromReq):
27+
var request: HTTPRequest
28+
var json: JSONType
29+
var a: Int
30+
31+
def __init__(out self, request: HTTPRequest, json: JSONType):
32+
self.a = 1
33+
self.request = request.copy()
34+
self.json = json.copy()
35+
36+
def __init__(out self, *, copy: Self):
37+
self.a = copy.a
38+
self.request = copy.request.copy()
39+
self.json = copy.json.copy()
40+
41+
def __str__(self) -> String:
42+
return String(self.a)
43+
44+
def from_request(mut self, req: HTTPRequest) raises -> Self:
45+
self.a = 2
46+
return self.copy()
47+
48+
49+
fn custom_request_payload(req: HTTPRequest) raises -> HandlerResponse:
50+
var payload = Payload(request=req, json=JSONType())
51+
payload = payload.from_request(req)
52+
print(payload.a)
53+
54+
# Returning a JSON as the response, this is a very limited placeholder for now
55+
var json_response = JSONType()
56+
json_response["a"] = String(payload.a)
57+
return json_response^
58+
59+
60+
fn main() raises:
61+
var app = App()
62+
63+
app.get("/", hello)
64+
65+
app.get("custom/", custom_request_payload)
66+
67+
app.post("/", printer)
68+
69+
var nested_router = Router("nested")
70+
nested_router.get(path="all/echo/", handler=nested)
71+
app.add_router(nested_router^)
72+
73+
app.start_server()

lightbug.🔥

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

lightbug_api/__init__.mojo

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,25 @@ from lightbug_api.routing import (
66
Router,
77
HandlerResponse,
88
JSONType,
9+
Handler,
910
)
1011

1112

12-
@value
1313
struct App:
1414
var router: RootRouter
1515

16-
fn __init__(inout self) raises:
16+
def __init__(out self) raises:
1717
self.router = RootRouter()
1818

19-
fn get[
20-
T: FromReq = BaseRequest
21-
](
22-
inout self,
23-
path: String,
24-
handler: fn (T) raises -> HandlerResponse,
25-
) raises:
26-
self.router.get[T](path, handler)
19+
def get(mut self, path: String, handler: Handler) raises:
20+
self.router.get(path, handler)
2721

28-
fn post[
29-
T: FromReq = BaseRequest
30-
](
31-
inout self,
32-
path: String,
33-
handler: fn (T) raises -> HandlerResponse,
34-
) raises:
35-
self.router.post[T](path, handler)
22+
def post(mut self, path: String, handler: Handler) raises:
23+
self.router.post(path, handler)
3624

37-
fn add_router(inout self, owned router: Router) raises -> None:
38-
self.router.add_router(router)
25+
def add_router(mut self, var router: Router) raises -> None:
26+
self.router.add_router(router^)
3927

40-
fn start_server(inout self, address: StringLiteral = "0.0.0.0:8080") raises:
28+
def start_server(mut self, address: String = "0.0.0.0:8080") raises:
4129
var server = Server()
4230
server.listen_and_serve(address, self.router)

0 commit comments

Comments
 (0)