|
1 | 1 | from lightbug_http import HTTPRequest, HTTPResponse, NotFound
|
2 | 2 |
|
| 3 | +alias allowed_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"] |
3 | 4 |
|
4 |
| -@register_passable("trivial") |
5 |
| -struct APIRoute: |
6 |
| - var path: StringLiteral |
7 |
| - var method: StringLiteral |
8 |
| - var handler: fn (HTTPRequest) -> HTTPResponse |
9 | 5 |
|
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"]() |
14 | 10 |
|
15 | 11 |
|
16 | 12 | @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]] |
19 | 21 |
|
20 | 22 | @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