Skip to content

Commit 86d11c9

Browse files
committed
Modified neopixel example to use Server.add_routes()
1 parent 4047ef5 commit 86d11c9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

examples/httpserver_neopixel.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import socketpool
88
import wifi
99

10-
from adafruit_httpserver import Server, Request, Response, GET, POST
10+
from adafruit_httpserver import Server, Route, Request, Response, GET, POST
1111

1212

1313
pool = socketpool.SocketPool(wifi.radio)
@@ -43,7 +43,6 @@ def change_neopixel_color_handler_post_body(request: Request):
4343
return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")
4444

4545

46-
@server.route("/change-neopixel-color/json", POST)
4746
def change_neopixel_color_handler_post_json(request: Request):
4847
"""Changes the color of the built-in NeoPixel using JSON POST body."""
4948

@@ -55,7 +54,6 @@ def change_neopixel_color_handler_post_json(request: Request):
5554
return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")
5655

5756

58-
@server.route("/change-neopixel-color/<r>/<g>/<b>", GET)
5957
def change_neopixel_color_handler_url_params(
6058
request: Request, r: str = "0", g: str = "0", b: str = "0"
6159
):
@@ -67,5 +65,15 @@ def change_neopixel_color_handler_url_params(
6765

6866
return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")
6967

68+
url_params_route = Route(
69+
"/change-neopixel-color/<r>/<g>/<b>", GET, change_neopixel_color_handler_url_params
70+
)
71+
72+
# Alternative way of registering routes.
73+
server.add_routes([
74+
Route("/change-neopixel-color/json", GET, change_neopixel_color_handler_post_json),
75+
url_params_route,
76+
])
77+
7078

7179
server.serve_forever(str(wifi.radio.ipv4_address))

0 commit comments

Comments
 (0)