# SPDX-FileCopyrightText: 2023 MichaĆ Pokusa
#
# SPDX-License-Identifier: Unlicense
import socketpool
import wifi
from adafruit_httpserver import Server, Request, Response, GET, POST
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, debug=True)
FORM_HTML_TEMPLATE = """
Form with {enctype} enctype
Form with {enctype} enctype
{submitted_value}
"""
@server.route("/form", [GET, POST])
def form(request: Request):
"""
Serve a form with the given enctype, and display back the submitted value.
"""
enctype = request.query_params.get("enctype", "text/plain")
if request.method == POST:
posted_value = request.form_data.get("something")
return Response(
request,
FORM_HTML_TEMPLATE.format(
enctype=enctype,
submitted_value=(
f"
Enctype: {enctype}
\n
Submitted form value: {posted_value}
"
if request.method == POST
else ""
),
),
content_type="text/html",
)
server.serve_forever(str(wifi.radio.ipv4_address))