@@ -3,7 +3,6 @@ from std.utils import Variant
33
44from lightbug_http import HTTPRequest, HTTPResponse, HTTPService, NotFound, OK
55from lightbug_http.http import RequestMethod
6- from lightbug_http.http.json import Json
76from lightbug_http.uri import URIDelimiters
87
98from lightbug_api.context import Context
@@ -22,11 +21,10 @@ struct RouterErrors:
2221
2322# ---------------------------------------------------------- public type aliases
2423
25- # The three things a handler may return:
26- # HTTPResponse — full control (status, headers, body)
24+ # The two things a handler may return:
25+ # HTTPResponse — full control (status, headers, body); use Response.* helpers
2726# String — auto-wrapped as 200 OK text/plain
28- # Json — auto-wrapped as 200 OK application/json
29- comptime HandlerResponse = Variant[HTTPResponse, String, Json]
27+ comptime HandlerResponse = Variant[HTTPResponse, String]
3028
3129# Every route handler shares this non-capturing function-pointer signature.
3230# Use Context to access the request, path params, query params, headers, body.
@@ -87,7 +85,7 @@ struct PathPattern(Copyable):
8785 # Strip the leading slash so we work in the same space as the
8886 # partial_path strings the router passes around.
8987 if len (path) > 0 and path.startswith(" /" ):
90- path = path[byte=1 :]
88+ path = String( path[byte=1 :])
9189
9290 # Root / empty pattern has no segments.
9391 if len (path) == 0 :
@@ -97,7 +95,7 @@ struct PathPattern(Copyable):
9795 for i in range (len (parts)):
9896 var s = String(parts[i])
9997 if s.startswith(" {" ) and s.endswith(" }" ):
100- var name = s[byte=1 : len (s) - 1 ]
98+ var name = String( s[byte=1 : len (s) - 1 ])
10199 segments.append(PathSegment(True , name))
102100 else :
103101 segments.append(PathSegment(False , s))
@@ -115,7 +113,7 @@ struct PathPattern(Copyable):
115113 """
116114 var check_path = path
117115 if len (check_path) > 0 and check_path.startswith(" /" ):
118- check_path = check_path[byte=1 :]
116+ check_path = String( check_path[byte=1 :])
119117
120118 # Empty pattern matches empty (root) path.
121119 if len (self .segments) == 0 :
@@ -149,11 +147,11 @@ struct RouteEntry(Copyable):
149147 fn __init__ (
150148 out self ,
151149 method : String,
152- pattern : PathPattern,
150+ var pattern : PathPattern,
153151 handler : Handler,
154152 ):
155153 self .method = method
156- self .pattern = pattern
154+ self .pattern = pattern^
157155 self .handler = handler
158156
159157 fn __init__ (out self , * , copy : Self):
@@ -311,7 +309,7 @@ struct RouterBase[is_main_app: Bool = False](HTTPService, Copyable):
311309 return NotFound(String(req.uri.path))
312310 raise e^
313311
314- var ctx = Context(req.copy(), route_match.path_params^ )
312+ var ctx = Context(req.copy(), route_match.path_params.copy() )
315313 var res = route_match.handler(ctx)
316314 return self ._encode_response(res^ )
317315
@@ -347,7 +345,8 @@ struct RouterBase[is_main_app: Bool = False](HTTPService, Copyable):
347345 continue
348346 var m = self .routes[i].pattern.match(partial_path)
349347 if m:
350- return RouteMatch(self .routes[i].handler, m.value()^ )
348+ var params = m.value().copy()
349+ return RouteMatch(self .routes[i].handler, params^ )
351350
352351 raise Error(RouterErrors.ROUTE_NOT_FOUND_ERROR )
353352
@@ -362,8 +361,6 @@ struct RouterBase[is_main_app: Bool = False](HTTPService, Copyable):
362361 return res.unsafe_take[HTTPResponse]()
363362 elif res.isa[String]():
364363 return OK(res[String])
365- elif res.isa[Json]():
366- return OK(res.unsafe_take[Json]())
367364 else :
368365 raise Error(" Unsupported HandlerResponse variant" )
369366
0 commit comments