Connect Interfaces
This commit is contained in:
parent
594d57c89c
commit
84ee2d376a
37
main.py
37
main.py
@ -1,5 +1,7 @@
|
|||||||
import aiohttp.web
|
import aiohttp.web
|
||||||
|
|
||||||
|
from .model import Model
|
||||||
|
|
||||||
routes = aiohttp.web.RouteTableDef()
|
routes = aiohttp.web.RouteTableDef()
|
||||||
|
|
||||||
routes.static('/static', 'static')
|
routes.static('/static', 'static')
|
||||||
@ -8,13 +10,12 @@ admintoken = "ae33fd8cc4fdcb1ff50ba42ff48046a7"
|
|||||||
|
|
||||||
CLIENT_REGEX = r'/{client:[a-zA-Z0-9]{16}}'
|
CLIENT_REGEX = r'/{client:[a-zA-Z0-9]{16}}'
|
||||||
|
|
||||||
meta_dict = {}
|
|
||||||
|
|
||||||
|
|
||||||
def get_client(request: aiohttp.web.Request):
|
def get_client(request: aiohttp.web.Request):
|
||||||
client = request.match_info.get("client", None)
|
client = request.match_info.get("client", None)
|
||||||
|
model = request.app['model']
|
||||||
|
|
||||||
if not client:
|
if not client or not model.exists_client(client):
|
||||||
raise aiohttp.web.HTTPBadRequest()
|
raise aiohttp.web.HTTPBadRequest()
|
||||||
|
|
||||||
return client
|
return client
|
||||||
@ -34,47 +35,48 @@ async def handler(request: aiohttp.web.Request):
|
|||||||
return aiohttp.web.FileResponse('ui.html')
|
return aiohttp.web.FileResponse('ui.html')
|
||||||
|
|
||||||
|
|
||||||
async def handle_request(data, client):
|
|
||||||
return "foo" in data
|
|
||||||
|
|
||||||
|
|
||||||
@routes.post(CLIENT_REGEX + '/api')
|
@routes.post(CLIENT_REGEX + '/api')
|
||||||
async def handler(request: aiohttp.web.Request):
|
async def handler(request: aiohttp.web.Request):
|
||||||
client = get_client(request)
|
client = get_client(request)
|
||||||
|
model = request.app['model']
|
||||||
|
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
|
|
||||||
print(f'{client=} {data=}')
|
print(f'{client=} {data=}')
|
||||||
|
|
||||||
ok = await handle_request(data, client)
|
try:
|
||||||
if ok:
|
assert await model.handle_post(data, client)
|
||||||
# return aiohttp.web.json_response({"x" : x})
|
|
||||||
return aiohttp.web.Response(status=200)
|
return aiohttp.web.Response(status=200)
|
||||||
else:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
return aiohttp.web.Response(status=400)
|
return aiohttp.web.Response(status=400)
|
||||||
|
|
||||||
|
|
||||||
@routes.get(CLIENT_REGEX + '/ws')
|
@routes.get(CLIENT_REGEX + '/ws')
|
||||||
async def _(request: aiohttp.web.Request):
|
async def _(request: aiohttp.web.Request):
|
||||||
client = get_client(request)
|
client = get_client(request)
|
||||||
|
model = request.app['model']
|
||||||
|
|
||||||
ws = aiohttp.web.WebSocketResponse(heartbeat=10)
|
ws = aiohttp.web.WebSocketResponse(heartbeat=10)
|
||||||
await ws.prepare(request)
|
await ws.prepare(request)
|
||||||
|
|
||||||
print(f'model.subscribe({client}, ws)')
|
print(f'[WS] client {client} connected, {ws=}')
|
||||||
|
model.subscribe(client, ws)
|
||||||
|
|
||||||
async for msg in ws:
|
async for msg in ws:
|
||||||
print(f'[WS] client sent {msg=}')
|
print(f'[WS] incoming message from client {client}, {ws=}, {msg=}')
|
||||||
|
|
||||||
print(f'model.unsubscribe(ws)')
|
print(f'[WS] client {client} disconnected, {ws=}')
|
||||||
|
model.unsubscribe(ws)
|
||||||
|
|
||||||
return ws
|
return ws
|
||||||
|
|
||||||
|
|
||||||
@routes.get(f"/api/{admintoken}")
|
@routes.get(f"/api/{admintoken}")
|
||||||
async def handler(request: aiohttp.web.Request):
|
async def handler(request: aiohttp.web.Request):
|
||||||
del request # unused
|
model = request.app['model']
|
||||||
|
|
||||||
return aiohttp.web.json_response(meta_dict)
|
return aiohttp.web.json_response(model.sessions)
|
||||||
|
|
||||||
|
|
||||||
@routes.get('/')
|
@routes.get('/')
|
||||||
@ -87,5 +89,8 @@ async def handler(request):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = aiohttp.web.Application()
|
app = aiohttp.web.Application()
|
||||||
app.add_routes(routes)
|
app.add_routes(routes)
|
||||||
|
|
||||||
|
app['model'] = Model()
|
||||||
|
|
||||||
aiohttp.web.run_app(app, port=42042)
|
aiohttp.web.run_app(app, port=42042)
|
||||||
print("should save state")
|
print("should save state")
|
||||||
|
Loading…
Reference in New Issue
Block a user