Skip to content

Commit 6d6bd70

Browse files
committed
parametrize the router
1 parent c16d345 commit 6d6bd70

File tree

6 files changed

+5960
-947
lines changed

6 files changed

+5960
-947
lines changed

lightbug.🔥

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from lightbug_api import App
21
from lightbug_http import HTTPRequest, HTTPResponse, OK
2+
from lightbug_api.app import App
3+
from lightbug_api.routing import APIRoute
34

45
@always_inline
56
fn printer(req: HTTPRequest) -> HTTPResponse:
@@ -8,12 +9,12 @@ fn printer(req: HTTPRequest) -> HTTPResponse:
89

910
@always_inline
1011
fn hello(req: HTTPRequest) -> HTTPResponse:
11-
return OK("Hello 🔥!")
12+
return OK("Hello, 🔥!", "text/plain; charset=utf-8")
1213

1314
fn main() raises:
14-
var app = App()
15-
16-
app.get("/", hello)
17-
app.post("/", printer)
15+
var app = App(
16+
APIRoute("/printer", "POST", printer),
17+
APIRoute("/hello", "GET", hello)
18+
)
1819

1920
app.start_server()

lightbug_api/__init__.mojo

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +0,0 @@
1-
from lightbug_http import HTTPRequest, HTTPResponse, SysServer, NotFound
2-
from lightbug_api.routing import Router
3-
4-
5-
@value
6-
struct App:
7-
var router: Router
8-
9-
fn __init__(inout self):
10-
self.router = Router()
11-
12-
fn func(self, req: HTTPRequest) raises -> HTTPResponse:
13-
for route_ptr in self.router.routes:
14-
var route = route_ptr[]
15-
if route.path == req.uri.path and route.method == req.method:
16-
return route.handler(req)
17-
return NotFound(req.uri.path)
18-
19-
fn get(inout self, path: String, handler: fn (HTTPRequest) -> HTTPResponse):
20-
self.router.add_route(path, "GET", handler)
21-
22-
fn post(inout self, path: String, handler: fn (HTTPRequest) -> HTTPResponse):
23-
self.router.add_route(path, "POST", handler)
24-
25-
fn start_server(inout self, address: StringLiteral = "0.0.0.0:8080") raises:
26-
var server = SysServer()
27-
server.listen_and_serve(address, self)

lightbug_api/app.mojo

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from lightbug_http import HTTPRequest, HTTPResponse, Server, NotFound
2+
from lightbug_api.routing import Router, APIRoute
3+
4+
@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+
11+
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
12+
for route in self.router.routes:
13+
if route.path == req.uri.path and route.method == req.method:
14+
return route.handler(req)
15+
return NotFound(req.uri.path)
16+
17+
fn start_server(inout self, address: StringLiteral = "0.0.0.0:8080") raises:
18+
var server = Server()
19+
server.listen_and_serve(address, self)

lightbug_api/routing.mojo

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

33

4-
@value
4+
@register_passable("trivial")
55
struct APIRoute:
6-
var path: String
7-
var method: String
6+
var path: StringLiteral
7+
var method: StringLiteral
88
var handler: fn (HTTPRequest) -> HTTPResponse
99

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
1014

11-
@value
12-
struct Router:
13-
var routes: List[APIRoute]
1415

15-
fn __init__(inout self):
16-
self.routes = List[APIRoute]()
16+
@register_passable("trivial")
17+
struct Router[*Ts: APIRoute]:
18+
var routes: VariadicList[APIRoute]
1719

18-
fn add_route(inout self, path: String, method: String, handler: fn (HTTPRequest) -> HTTPResponse):
19-
self.routes.append(APIRoute(path, method, handler))
20+
@always_inline
21+
fn __init__(inout self, routes: VariadicList[APIRoute]):
22+
self.routes = routes

0 commit comments

Comments
 (0)