webpnp/main.py
Kai Vogelgesang a487c3163e Refactor
2020-04-16 14:58:37 +02:00

55 lines
1.2 KiB
Python

import aiohttp.web
routes = aiohttp.web.RouteTableDef()
routes.static('/static', 'static')
admintoken = "ae33fd8cc4fdcb1ff50ba42ff48046a7"
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", "")
print(f"{client=} accessed")
return aiohttp.web.FileResponse('ui.html')
async def handle_request(data, client):
return "foo" in data
@routes.post(CLIENT_REGEX + '/api')
async def handler(request):
client = request.match_info.get("client", "")
data = await request.post()
ok = await handle_request(data, client)
if ok:
# return aiohttp.web.json_response({"x" : x})
return aiohttp.web.Response(status=200)
else:
return aiohttp.web.Response(status=400)
@routes.get(f"/api/{admintoken}")
async def handler(request):
return aiohttp.web.json_response(meta_dict)
@routes.get('/')
async def handler(request):
del request # unused
return aiohttp.web.FileResponse('index.html')
if __name__ == '__main__':
app = aiohttp.web.Application()
app.add_routes(routes)
aiohttp.web.run_app(app, port=42042)
print("should save state")