Skip to content

Commit 9a07ca1

Browse files
committed
one route works
1 parent b51e2ed commit 9a07ca1

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
*.egg-info
1111

1212
# magic environments
13-
.magic
13+
.magic
14+
15+
.lightbug

lightbug.🔥

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ from lightbug_http import HTTPRequest, HTTPResponse, OK
22
from lightbug_api.app import App
33
from lightbug_api.routing import Router, APIRoute
44

5+
56
@always_inline
67
fn printer(req: HTTPRequest) -> HTTPResponse:
78
print("Got a request on ", req.uri.path, " with method ", req.method)
89
return OK(req.body_raw)
910

11+
1012
@always_inline
1113
fn hello(req: HTTPRequest) -> HTTPResponse:
1214
return OK("Hello, 🔥!", "text/plain; charset=utf-8")
1315

14-
fn main() raises:
15-
var router =
16-
Router[
17-
APIRoute["/printer", "POST", printer],
18-
APIRoute["/hello", "GET", hello]
19-
]()
2016

21-
# app.start_server()
17+
fn main() raises:
18+
alias router = Router[
19+
APIRoute["/", "GET", hello](),
20+
# APIRoute["/print", "POST", printer]()
21+
]()
22+
var app = App[router]()
23+
24+
app.start_server()

lightbug_api/app.mojo

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ from lightbug_http import HTTPRequest, HTTPResponse, Server, NotFound
22
from lightbug_api.routing import Router, APIRoute
33

44
@register_passable("trivial")
5-
struct App[*Ts: APIRoute, router: Router]:
5+
struct App[router: Router]:
6+
fn __init__(out self):
7+
pass
8+
69
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
7-
for route in router.routes:
10+
for route in VariadicList(router.routes):
811
if route.path == req.uri.path and route.method == req.method:
912
return route.handler(req)
1013
return NotFound(req.uri.path)
1114

12-
fn start_server(inout self, address: StringLiteral = "0.0.0.0:8080") raises:
15+
fn start_server(mut self, address: String = "0.0.0.0:8080") raises:
1316
var server = Server()
1417
server.listen_and_serve(address, self)

lightbug_api/routing.mojo

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from lightbug_http import HTTPRequest, HTTPResponse, NotFound
22

3+
34
alias allowed_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
45

56

@@ -9,16 +10,8 @@ struct APIRoute[path: StringLiteral, method: StringLiteral, handler: fn (HTTPReq
910
constrained[method in allowed_methods, "Invalid method"]()
1011

1112

12-
@register_passable("trivial")
1313
struct Router[
14-
path: StringLiteral,
15-
method: StringLiteral,
16-
handler: fn (HTTPRequest) -> HTTPResponse,
17-
//,
18-
*Routes: APIRoute[path, method, handler]
14+
*routes: APIRoute,
1915
]:
20-
var routes: VariadicList[APIRoute[path, method, handler]]
21-
22-
@always_inline
23-
fn __init__(inout self, *routes: APIRoute[path, method, handler]):
24-
self.routes = VariadicList(routes)
16+
fn __init__(out self):
17+
pass

0 commit comments

Comments
 (0)