Skip to content

Commit fa0583a

Browse files
committed
get it to compile
1 parent 9273dd4 commit fa0583a

3 files changed

Lines changed: 15 additions & 20 deletions

File tree

lightbug.mojo

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from lightbug_api import App, Router, HandlerResponse
1919
from lightbug_api.context import Context
2020
from lightbug_api.response import Response
21-
from lightbug_http.http.json import Json, JsonSerializable, JsonDeserializable
21+
from lightbug_http.http.json import JsonSerializable, JsonDeserializable
2222

2323

2424
# ----------------------------------------------------------------- data types
@@ -80,8 +80,7 @@ fn get_item(ctx: Context) raises -> HandlerResponse:
8080
if verbose == "true":
8181
print("GET /items/", id, " (verbose mode)")
8282

83-
# Demonstrate that a bare Json value also works as HandlerResponse.
84-
return Json(Item(42, String("Item ", id), 9.99))
83+
return Response.json(Item(42, String("Item ", id), 9.99))
8584

8685

8786
fn create_item(ctx: Context) raises -> HandlerResponse:
@@ -93,7 +92,6 @@ fn create_item(ctx: Context) raises -> HandlerResponse:
9392

9493
fn update_item(ctx: Context) raises -> HandlerResponse:
9594
"""PUT /items/{id} — update an item."""
96-
var id = ctx.path_param("id", "0")
9795
var body = ctx.json[CreateItemRequest]()
9896
return Response.json(Item(42, body.name, body.price))
9997

lightbug_api/response.mojo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ struct Response:
5353
Parameters:
5454
T: Any type supported by ``emberjson.serialize``.
5555
"""
56-
var resp = HTTPResponse(Json(value)^)
56+
var resp = HTTPResponse(Json(value))
5757
resp.status_code = 201
5858
resp.status_text = "Created"
5959
return resp^
6060

6161
@staticmethod
6262
fn accepted[T: AnyType](value: T) -> HTTPResponse:
6363
"""202 Accepted — serialize *value* as ``application/json``."""
64-
var resp = HTTPResponse(Json(value)^)
64+
var resp = HTTPResponse(Json(value))
6565
resp.status_code = 202
6666
resp.status_text = "Accepted"
6767
return resp^

lightbug_api/routing.mojo

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from std.utils import Variant
33

44
from lightbug_http import HTTPRequest, HTTPResponse, HTTPService, NotFound, OK
55
from lightbug_http.http import RequestMethod
6-
from lightbug_http.http.json import Json
76
from lightbug_http.uri import URIDelimiters
87

98
from 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

Comments
 (0)