File tree 4 files changed +10
-7
lines changed
4 files changed +10
-7
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ from lightbug_http.middleware.helpers import Success
3
3
from sys import is_defined
4
4
from lightbug_http import *
5
5
6
+ @value
6
7
struct HelloWorld(HTTPHandler):
7
8
fn handle(self, context: Context) -> HTTPResponse:
8
9
var name = context.params.get("username", "world")
Original file line number Diff line number Diff line change
1
+ from lightbug_http.http import HTTPRequest, HTTPResponse, ResponseHeader
2
+
1
3
# ## Helper functions to create HTTP responses
2
4
fn Success (body : String) -> HTTPResponse:
3
5
return Success(body, String(" text/plain" ))
Original file line number Diff line number Diff line change @@ -6,13 +6,13 @@ from lightbug_http.middleware import *
6
6
# # It is passed to each middleware in the chain.
7
7
# # It also contains a dictionary of parameters that can be shared between middleware.
8
8
@value
9
- struct Context :
9
+ struct Context[ParamType: CollectionElement] :
10
10
var request : HTTPRequest
11
- var params : Dict[String, AnyType ]
11
+ var params : Dict[String, ParamType ]
12
12
13
13
fn __init__ (inout self , request : HTTPRequest):
14
14
self .request = request
15
- self .params = Dict[String, AnyType ]()
15
+ self .params = Dict[String, ParamType ]()
16
16
17
17
18
18
# # Middleware is an interface for processing HTTP requests.
Original file line number Diff line number Diff line change 1
1
# # HTTPHandler is an interface for handling HTTP requests in the RouterMiddleware.
2
2
# # It is a leaf node in the middleware chain.
3
- trait HTTPHandler :
3
+ trait HTTPHandler ( CollectionElement ) :
4
4
fn handle (self , context : Context) -> HTTPResponse:
5
5
...
6
6
7
7
8
8
# # Router middleware routes requests to different middleware based on the path.
9
9
@value
10
- struct RouterMiddleware (Middleware ):
10
+ struct RouterMiddleware[HTTPHandlerType: HTTPHandler] (Middleware):
11
11
var next : Middleware
12
- var routes : Dict[String, HTTPHandler ]
12
+ var routes : Dict[String, HTTPHandlerType ]
13
13
14
14
fn __init__ (inout self ):
15
- self .routes = Dict[String, HTTPHandler ]()
15
+ self .routes = Dict[String, HTTPHandlerType ]()
16
16
17
17
fn set_next (self , next : Middleware):
18
18
self .next = next
You can’t perform that action at this time.
0 commit comments