Boilerplate
This commit is contained in:
parent
c4ffa25b33
commit
e8900515bb
44
main.py
44
main.py
@ -11,9 +11,18 @@ CLIENT_REGEX = r'/{client:[a-zA-Z0-9]{16}}'
|
||||
meta_dict = {}
|
||||
|
||||
|
||||
@routes.get(CLIENT_REGEX)
|
||||
async def handler(request):
|
||||
client = request.match_info.get("client", "")
|
||||
def get_client(request: aiohttp.web.Request):
|
||||
client = request.match_info.get("client", None)
|
||||
|
||||
if not client:
|
||||
raise aiohttp.web.HTTPBadRequest()
|
||||
|
||||
return client
|
||||
|
||||
|
||||
@routes.get(CLIENT_REGEX + '/')
|
||||
async def handler(request: aiohttp.web.Request):
|
||||
client = get_client(request)
|
||||
print(f"{client=} accessed")
|
||||
return aiohttp.web.FileResponse('ui.html')
|
||||
|
||||
@ -23,10 +32,11 @@ async def handle_request(data, client):
|
||||
|
||||
|
||||
@routes.post(CLIENT_REGEX + '/api')
|
||||
async def handler(request):
|
||||
client = request.match_info.get("client", "")
|
||||
async def handler(request: aiohttp.web.Request):
|
||||
client = get_client(request)
|
||||
data = await request.json()
|
||||
|
||||
data = await request.post()
|
||||
print(f'{client=} {data=}')
|
||||
|
||||
ok = await handle_request(data, client)
|
||||
if ok:
|
||||
@ -36,14 +46,34 @@ async def handler(request):
|
||||
return aiohttp.web.Response(status=400)
|
||||
|
||||
|
||||
@routes.get(CLIENT_REGEX + '/ws')
|
||||
async def _(request: aiohttp.web.Request):
|
||||
client = get_client(request)
|
||||
|
||||
ws = aiohttp.web.WebSocketResponse(heartbeat=10)
|
||||
await ws.prepare(request)
|
||||
|
||||
print(f'model.subscribe({client}, ws)')
|
||||
|
||||
async for msg in ws:
|
||||
print(f'[WS] client sent {msg=}')
|
||||
|
||||
print(f'model.unsubscribe(ws)')
|
||||
|
||||
return ws
|
||||
|
||||
|
||||
@routes.get(f"/api/{admintoken}")
|
||||
async def handler(request):
|
||||
async def handler(request: aiohttp.web.Request):
|
||||
del request # unused
|
||||
|
||||
return aiohttp.web.json_response(meta_dict)
|
||||
|
||||
|
||||
@routes.get('/')
|
||||
async def handler(request):
|
||||
del request # unused
|
||||
|
||||
return aiohttp.web.FileResponse('index.html')
|
||||
|
||||
|
||||
|
9
static/renderer.js
Normal file
9
static/renderer.js
Normal file
@ -0,0 +1,9 @@
|
||||
const ws_url = new URL('ws', window.location.href);
|
||||
ws_url.protocol = ws_url.protocol.replace('http', 'ws');
|
||||
const ws = new WebSocket(ws_url.href);
|
||||
|
||||
ws.onmessage = function(event) {
|
||||
const msg = JSON.parse(event.data);
|
||||
|
||||
console.log(msg);
|
||||
};
|
46
ui.html
46
ui.html
@ -3,39 +3,25 @@
|
||||
|
||||
<head>
|
||||
<title>leafblade Minecraft Server</title>
|
||||
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<style type="text/css">
|
||||
html,
|
||||
body {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
#map {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
@media (max-width:768px) {
|
||||
#overlay { width: 100% }
|
||||
}
|
||||
</style>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
</script>
|
||||
|
||||
<meta charset="UTF-8"/>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="container"></div>
|
||||
this is the UI
|
||||
</div>
|
||||
this is the UI
|
||||
|
||||
<button id="test_api">Test API</button>
|
||||
|
||||
<script src="/static/renderer.js"></script>
|
||||
<script>
|
||||
document.getElementById("test_api").onclick = async function (e) {
|
||||
let data = {foo: 'bar'};
|
||||
|
||||
await fetch('api', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user