44# Test: curl http://localhost:8080/
55# curl http://localhost:8080/items
66# curl http://localhost:8080/items/42
7- # curl http://localhost:8080/items/42?verbose=true
7+ # curl " http://localhost:8080/items/42?verbose=true"
88# curl -X POST http://localhost:8080/items \
99# -H 'Content-Type: application/json' \
1010# -d '{"name":"Widget","price":9.99}'
1515# curl http://localhost:8080/v1/status
1616# ─────────────────────────────────────────────────────────────────────────────
1717
18- from lightbug_api import App, Router , HandlerResponse
18+ from lightbug_api import App, GET , POST , PUT , DELETE , mount , HandlerResponse
1919from lightbug_api.context import Context
2020from lightbug_api.response import Response
2121from lightbug_http.http.json import JsonSerializable, JsonDeserializable
@@ -25,8 +25,6 @@ from lightbug_http.http.json import JsonSerializable, JsonDeserializable
2525
2626@fieldwise_init
2727struct Item (JsonSerializable , Movable , Defaultable ):
28- """ An item returned in API responses."""
29-
3028 var id : Int
3129 var name : String
3230 var price : Float64
@@ -39,8 +37,6 @@ struct Item(JsonSerializable, Movable, Defaultable):
3937
4038@fieldwise_init
4139struct CreateItemRequest (JsonDeserializable , Movable , Defaultable ):
42- """ JSON body expected for POST /items."""
43-
4440 var name : String
4541 var price : Float64
4642
@@ -62,70 +58,59 @@ struct StatusResponse(JsonSerializable, Movable, Defaultable):
6258# ------------------------------------------------------------------ handlers
6359
6460fn index (ctx : Context) raises -> HandlerResponse:
65- """ GET / — plain-text welcome message."""
6661 return Response.text(" Welcome to lightbug_api 🔥" )
6762
6863
6964fn list_items (ctx : Context) raises -> HandlerResponse:
70- """ GET /items — return a hard-coded list as JSON."""
71- # In a real app you'd query a database here.
7265 return Response.json(Item(1 , " Widget" , 9.99 ))
7366
7467
7568fn get_item (ctx : Context) raises -> HandlerResponse:
76- """ GET /items/{id} — return one item by ID."""
77- var id = ctx.path_param(" id" , " unknown" )
78- var verbose = ctx.query(" verbose" , " false" )
69+ # ctx.param("id", 0) → Int (path param, typed by default value)
70+ # ctx.query("verbose", False) → Bool (query param, typed by default value)
71+ var id = ctx.param(" id" , 0 )
72+ var verbose = ctx.query(" verbose" , False )
7973
80- if verbose == " true " :
74+ if verbose:
8175 print (" GET /items/" , id , " (verbose mode)" )
8276
83- return Response.json(Item(42 , String(" Item " , id ), 9.99 ))
77+ return Response.json(Item(id , String(" Item " , id ), 9.99 ))
8478
8579
8680fn create_item (ctx : Context) raises -> HandlerResponse:
87- """ POST /items — deserialize JSON body, return 201 Created."""
88- var body = ctx.json[CreateItemRequest]()
81+ var body = ctx.json[CreateItemRequest]()
8982 var created = Item(100 , body.name, body.price)
9083 return Response.created(created)
9184
9285
9386fn update_item (ctx : Context) raises -> HandlerResponse:
94- """ PUT /items/{id} — update an item."""
9587 var body = ctx.json[CreateItemRequest]()
96- return Response.json(Item(42 , body.name, body.price))
88+ var id = ctx.param(" id" , 0 )
89+ return Response.json(Item(id , body.name, body.price))
9790
9891
9992fn delete_item (ctx : Context) raises -> HandlerResponse:
100- """ DELETE /items/{id} — delete an item, return 204 No Content."""
101- var id = ctx.path_param(" id" , " 0" )
93+ var id = ctx.param(" id" , 0 )
10294 print (" Deleting item" , id )
10395 return Response.no_content()
10496
10597
10698fn health (ctx : Context) raises -> HandlerResponse:
107- """ GET /v1/status — health check mounted under the v1 sub-router."""
10899 return Response.json(StatusResponse(" ok" , " 1.0.0" ))
109100
110101
111102# --------------------------------------------------------------------- main
112103
113104fn main () raises :
114- var app = App()
115-
116- # Root
117- app.get(" /" , index)
118-
119- # Items resource — all HTTP verbs
120- app.get(" /items" , list_items)
121- app.get(" /items/{id} " , get_item)
122- app.post(" /items" , create_item)
123- app.put(" /items/{id} " , update_item)
124- app.delete(" /items/{id} " , delete_item)
125-
126- # Sub-router mounted at /v1
127- var v1 = Router(" v1" )
128- v1.get(" status" , health)
129- app.add_router(v1^ )
130-
105+ var app = App(
106+ GET(" /" , index),
107+ GET(" /items" , list_items),
108+ GET(" /items/{id} " , get_item),
109+ POST(" /items" , create_item),
110+ PUT(" /items/{id} " , update_item),
111+ DELETE(" /items/{id} " , delete_item),
112+ mount(" v1" ,
113+ GET(" status" , health),
114+ ),
115+ )
131116 app.run()
0 commit comments