Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router compile-time parametrization #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lightbug.🔥
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from lightbug_api import App
from lightbug_http import HTTPRequest, HTTPResponse, OK
from lightbug_api.app import App
from lightbug_api.routing import Router, APIRoute

@always_inline
fn printer(req: HTTPRequest) -> HTTPResponse:
Expand All @@ -8,12 +9,13 @@ fn printer(req: HTTPRequest) -> HTTPResponse:

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

fn main() raises:
var app = App()
var router =
Router[
APIRoute["/printer", "POST", printer],
APIRoute["/hello", "GET", hello]
]()

app.get("/", hello)
app.post("/", printer)

app.start_server()
# app.start_server()
27 changes: 0 additions & 27 deletions lightbug_api/__init__.mojo
Original file line number Diff line number Diff line change
@@ -1,27 +0,0 @@
from lightbug_http import HTTPRequest, HTTPResponse, SysServer, NotFound
from lightbug_api.routing import Router


@value
struct App:
var router: Router

fn __init__(inout self):
self.router = Router()

fn func(self, req: HTTPRequest) raises -> HTTPResponse:
for route_ptr in self.router.routes:
var route = route_ptr[]
if route.path == req.uri.path and route.method == req.method:
return route.handler(req)
return NotFound(req.uri.path)

fn get(inout self, path: String, handler: fn (HTTPRequest) -> HTTPResponse):
self.router.add_route(path, "GET", handler)

fn post(inout self, path: String, handler: fn (HTTPRequest) -> HTTPResponse):
self.router.add_route(path, "POST", handler)

fn start_server(inout self, address: StringLiteral = "0.0.0.0:8080") raises:
var server = SysServer()
server.listen_and_serve(address, self)
14 changes: 14 additions & 0 deletions lightbug_api/app.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from lightbug_http import HTTPRequest, HTTPResponse, Server, NotFound
from lightbug_api.routing import Router, APIRoute

@register_passable("trivial")
struct App[*Ts: APIRoute, router: Router]:
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
for route in router.routes:
if route.path == req.uri.path and route.method == req.method:
return route.handler(req)
return NotFound(req.uri.path)

fn start_server(inout self, address: StringLiteral = "0.0.0.0:8080") raises:
var server = Server()
server.listen_and_serve(address, self)
29 changes: 17 additions & 12 deletions lightbug_api/routing.mojo
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from lightbug_http import HTTPRequest, HTTPResponse, NotFound

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

@value
struct APIRoute:
var path: String
var method: String
var handler: fn (HTTPRequest) -> HTTPResponse

@register_passable("trivial")
struct APIRoute[path: StringLiteral, method: StringLiteral, handler: fn (HTTPRequest) -> HTTPResponse]:
fn __init__(out self):
constrained[method in allowed_methods, "Invalid method"]()

@value
struct Router:
var routes: List[APIRoute]

fn __init__(inout self):
self.routes = List[APIRoute]()
@register_passable("trivial")
struct Router[
path: StringLiteral,
method: StringLiteral,
handler: fn (HTTPRequest) -> HTTPResponse,
//,
*Routes: APIRoute[path, method, handler]
]:
var routes: VariadicList[APIRoute[path, method, handler]]

fn add_route(inout self, path: String, method: String, handler: fn (HTTPRequest) -> HTTPResponse):
self.routes.append(APIRoute(path, method, handler))
@always_inline
fn __init__(inout self, *routes: APIRoute[path, method, handler]):
self.routes = VariadicList(routes)
Loading
Loading