Skip to content

Commit b51e2ed

Browse files
committed
wip inferred parameters
1 parent 2eae766 commit b51e2ed

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

lightbug.🔥

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from lightbug_http import HTTPRequest, HTTPResponse, OK
22
from lightbug_api.app import App
3-
from lightbug_api.routing import APIRoute
3+
from lightbug_api.routing import Router, APIRoute
44

55
@always_inline
66
fn printer(req: HTTPRequest) -> HTTPResponse:
@@ -12,9 +12,10 @@ fn hello(req: HTTPRequest) -> HTTPResponse:
1212
return OK("Hello, 🔥!", "text/plain; charset=utf-8")
1313

1414
fn main() raises:
15-
var app = App(
16-
APIRoute("/printer", "POST", printer),
17-
APIRoute("/hello", "GET", hello)
18-
)
15+
var router =
16+
Router[
17+
APIRoute["/printer", "POST", printer],
18+
APIRoute["/hello", "GET", hello]
19+
]()
1920

20-
app.start_server()
21+
# app.start_server()

lightbug_api/app.mojo

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ 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]:
6-
var router: Router[*Ts]
7-
8-
fn __init__(out self, *routes: APIRoute):
9-
self.router = Router[*Ts](routes)
10-
5+
struct App[*Ts: APIRoute, router: Router]:
116
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
12-
for route in self.router.routes:
7+
for route in router.routes:
138
if route.path == req.uri.path and route.method == req.method:
149
return route.handler(req)
1510
return NotFound(req.uri.path)

lightbug_api/routing.mojo

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

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

4-
@register_passable("trivial")
5-
struct APIRoute:
6-
var path: StringLiteral
7-
var method: StringLiteral
8-
var handler: fn (HTTPRequest) -> HTTPResponse
95

10-
fn __init__(out self, path: StringLiteral, method: StringLiteral, handler: fn (HTTPRequest) -> HTTPResponse):
11-
self.path = path
12-
self.method = method
13-
self.handler = handler
6+
@register_passable("trivial")
7+
struct APIRoute[path: StringLiteral, method: StringLiteral, handler: fn (HTTPRequest) -> HTTPResponse]:
8+
fn __init__(out self):
9+
constrained[method in allowed_methods, "Invalid method"]()
1410

1511

1612
@register_passable("trivial")
17-
struct Router[*Ts: APIRoute]:
18-
var routes: VariadicList[APIRoute]
13+
struct Router[
14+
path: StringLiteral,
15+
method: StringLiteral,
16+
handler: fn (HTTPRequest) -> HTTPResponse,
17+
//,
18+
*Routes: APIRoute[path, method, handler]
19+
]:
20+
var routes: VariadicList[APIRoute[path, method, handler]]
1921

2022
@always_inline
21-
fn __init__(inout self, routes: VariadicList[APIRoute]):
22-
self.routes = routes
23+
fn __init__(inout self, *routes: APIRoute[path, method, handler]):
24+
self.routes = VariadicList(routes)

0 commit comments

Comments
 (0)