Update main.py with controllers and static route
- Mount model controllers as subroutes - Add static file serving route - Integrate controllers into main application
This commit is contained in:
59
src/main.py
59
src/main.py
@@ -1,14 +1,65 @@
|
||||
import asyncio
|
||||
from settings import Settings
|
||||
from web import web
|
||||
import gc
|
||||
import machine
|
||||
from microdot import Microdot, send_file
|
||||
from microdot.websocket import with_websocket
|
||||
|
||||
import aioespnow
|
||||
import network
|
||||
from controllers.preset import preset
|
||||
import controllers.profile as profile
|
||||
import controllers.group as group
|
||||
import controllers.sequence as sequence
|
||||
import controllers.tab as tab
|
||||
import controllers.palette as palette
|
||||
|
||||
|
||||
|
||||
async def main():
|
||||
settings = Settings()
|
||||
print("Starting")
|
||||
w = web(settings)
|
||||
server = asyncio.create_task(w.start_server(host="0.0.0.0", port=80))
|
||||
|
||||
network.WLAN(network.STA_IF).active(True)
|
||||
|
||||
|
||||
e = aioespnow.AIOESPNow()
|
||||
e.active(True)
|
||||
e.add_peer(b"\xbb\xbb\xbb\xbb\xbb\xbb")
|
||||
|
||||
app = Microdot()
|
||||
|
||||
# Mount model controllers as subroutes
|
||||
app.mount('/presets', preset.controller)
|
||||
app.mount('/profiles', profile.controller)
|
||||
app.mount('/groups', group.controller)
|
||||
app.mount('/sequences', sequence.controller)
|
||||
app.mount('/tabs', tab.controller)
|
||||
app.mount('/palettes', palette.controller)
|
||||
|
||||
# Static file route
|
||||
@app.route("/static/<path:path>")
|
||||
def static_handler(request, path):
|
||||
"""Serve static files."""
|
||||
if '..' in path:
|
||||
# Directory traversal is not allowed
|
||||
return 'Not found', 404
|
||||
return send_file('static/' + path)
|
||||
|
||||
@app.route('/ws')
|
||||
@with_websocket
|
||||
async def ws(request, ws):
|
||||
while True:
|
||||
data = await ws.receive()
|
||||
if data:
|
||||
await e.asend(b"\xbb\xbb\xbb\xbb\xbb\xbb", data)
|
||||
print(data)
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
|
||||
server = asyncio.create_task(app.start_server(host="0.0.0.0", port=80))
|
||||
|
||||
wdt = machine.WDT(timeout=10000)
|
||||
wdt.feed()
|
||||
@@ -19,5 +70,5 @@ async def main():
|
||||
wdt.feed()
|
||||
await asyncio.sleep_ms(500)
|
||||
# cleanup before ending the application
|
||||
await server
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user