File tree Expand file tree Collapse file tree 4 files changed +10
-7
lines changed Expand file tree Collapse file tree 4 files changed +10
-7
lines changed Original file line number Diff line number Diff line change 11from sys import is_defined
22from lightbug_http import *
33
4+ @value
45struct HelloWorld(HTTPHandler):
56 fn handle(self, context: Context) -> HTTPResponse:
67 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+
13# ## Helper functions to create HTTP responses
24fn Success (body : String) -> HTTPResponse:
35 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 *
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.
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments