Skip to content

Commit 2b21143

Browse files
savioranddrujensen
authored andcommitted
fix trait parameter
1 parent 7b28007 commit 2b21143

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

lightbug.🔥

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from lightbug_http.middleware.helpers import Success
33
from sys import is_defined
44
from lightbug_http import *
55

6+
@value
67
struct HelloWorld(HTTPHandler):
78
fn handle(self, context: Context) -> HTTPResponse:
89
var name = context.params.get("username", "world")

lightbug_http/middleware/helpers.mojo

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from lightbug_http.http import HTTPRequest, HTTPResponse, ResponseHeader
2+
13
### Helper functions to create HTTP responses
24
fn Success(body: String) -> HTTPResponse:
35
return Success(body, String("text/plain"))

lightbug_http/middleware/middleware.mojo

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ from lightbug_http.middleware import *
66
## It is passed to each middleware in the chain.
77
## It also contains a dictionary of parameters that can be shared between middleware.
88
@value
9-
struct Context:
9+
struct Context[ParamType: CollectionElement]:
1010
var request: HTTPRequest
11-
var params: Dict[String, AnyType]
11+
var params: Dict[String, ParamType]
1212

1313
fn __init__(inout self, request: HTTPRequest):
1414
self.request = request
15-
self.params = Dict[String, AnyType]()
15+
self.params = Dict[String, ParamType]()
1616

1717

1818
## Middleware is an interface for processing HTTP requests.

lightbug_http/middleware/router.mojo

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
## HTTPHandler is an interface for handling HTTP requests in the RouterMiddleware.
22
## It is a leaf node in the middleware chain.
3-
trait HTTPHandler:
3+
trait HTTPHandler(CollectionElement):
44
fn handle(self, context: Context) -> HTTPResponse:
55
...
66

77

88
## Router middleware routes requests to different middleware based on the path.
99
@value
10-
struct RouterMiddleware(Middleware):
10+
struct RouterMiddleware[HTTPHandlerType: HTTPHandler](Middleware):
1111
var next: Middleware
12-
var routes: Dict[String, HTTPHandler]
12+
var routes: Dict[String, HTTPHandlerType]
1313

1414
fn __init__(inout self):
15-
self.routes = Dict[String, HTTPHandler]()
15+
self.routes = Dict[String, HTTPHandlerType]()
1616

1717
fn set_next(self, next: Middleware):
1818
self.next = next

0 commit comments

Comments
 (0)