44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from microdot import Microdot, send_file, Response
|
|
from microdot.utemplate import Template
|
|
from microdot.websocket import with_websocket
|
|
import machine
|
|
from settings import set_settings
|
|
import wifi
|
|
import json
|
|
|
|
def web(settings, patterns):
|
|
app = Microdot()
|
|
Response.default_content_type = 'text/html'
|
|
|
|
@app.route('/')
|
|
async def index_hnadler(request):
|
|
mac = wifi.get_mac().hex()
|
|
return Template('/index.html').render(settings=settings, patterns=patterns.patterns.keys(), mac=mac)
|
|
|
|
@app.route("/static/<path:path>")
|
|
def static_handler(request, path):
|
|
if '..' in path:
|
|
# Directory traversal is not allowed
|
|
return 'Not found', 404
|
|
return send_file('static/' + path)
|
|
|
|
@app.post("/settings")
|
|
def settings_handler(request):
|
|
# Keep the POST handler for compatibility or alternative usage if needed
|
|
# For WebSocket updates, the /ws handler is now primary
|
|
return set_settings(request.body.decode('utf-8'), settings, patterns)
|
|
|
|
@app.route("/ws")
|
|
@with_websocket
|
|
async def ws(request, ws):
|
|
while True:
|
|
data = await ws.receive()
|
|
if data:
|
|
# Process the received data
|
|
_, status_code = set_settings(data, settings, patterns)
|
|
await ws.send(status_code)
|
|
else:
|
|
break
|
|
|
|
return app
|