forked from adafruit/Adafruit_CircuitPython_HTTPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver_cookies.py
86 lines (69 loc) · 2.16 KB
/
httpserver_cookies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# SPDX-FileCopyrightText: 2023 Michał Pokusa
#
# SPDX-License-Identifier: Unlicense
import socketpool
import wifi
from adafruit_httpserver import Server, Request, Response, GET, Headers
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, debug=True)
THEMES = {
"dark": {
"background-color": "#1c1c1c",
"color": "white",
"button-color": "#181818",
},
"light": {
"background-color": "white",
"color": "#1c1c1c",
"button-color": "white",
},
}
def themed_template(user_preferred_theme: str):
theme = THEMES[user_preferred_theme]
return f"""
<html>
<head>
<title>Cookie Example</title>
<style>
body {{
background-color: {theme['background-color']};
color: {theme['color']};
}}
button {{
background-color: {theme['button-color']};
color: {theme['color']};
border: 1px solid {theme['color']};
padding: 10px;
margin: 10px;
}}
</style>
</head>
<body>
<a href="/?theme=dark"><button>Dark theme</button></a>
<a href="/?theme=light"><button>Light theme</button></a>
<br />
<p>
After changing the theme, close the tab and open again.
Notice that theme stays the same.
</p>
</body>
</html>
"""
@server.route("/", GET)
def themed_from_cookie(request: Request):
"""
Serve a simple themed page, based on the user's cookie.
"""
user_theme = request.cookies.get("theme", "light")
wanted_theme = request.query_params.get("theme", user_theme)
headers = Headers()
headers.add("Set-Cookie", "cookie1=value1")
headers.add("Set-Cookie", "cookie2=value2")
return Response(
request,
themed_template(wanted_theme),
content_type="text/html",
headers=headers,
cookies={} if user_theme == wanted_theme else {"theme": wanted_theme},
)
server.serve_forever(str(wifi.radio.ipv4_address))