Draft Login
This commit is contained in:
parent
b5cee96d7a
commit
b736a0b65c
@ -33,9 +33,9 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="container"></div>
|
||||
this is the base site
|
||||
</div>
|
||||
|
||||
<a href="/newclient">Log in</a>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
16
main.py
16
main.py
@ -15,9 +15,15 @@ def get_client(request: aiohttp.web.Request):
|
||||
client = request.match_info.get("client", None)
|
||||
model = request.app['model']
|
||||
|
||||
if not client or not model.exists_client(client):
|
||||
if not client:
|
||||
print('[get_client] client is not set, wtf')
|
||||
raise aiohttp.web.HTTPBadRequest()
|
||||
|
||||
if not model.exists_client(client):
|
||||
print(f'[get_client] model does not know {client=}')
|
||||
raise aiohttp.web.HTTPBadRequest()
|
||||
|
||||
|
||||
return client
|
||||
|
||||
|
||||
@ -86,6 +92,14 @@ async def handler(request):
|
||||
return aiohttp.web.FileResponse('index.html')
|
||||
|
||||
|
||||
@routes.get('/newclient')
|
||||
async def handler(request):
|
||||
model = request.app['model']
|
||||
|
||||
client = model.create_client()
|
||||
raise aiohttp.web.HTTPFound(f"/{client}/")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = aiohttp.web.Application()
|
||||
app.add_routes(routes)
|
||||
|
11
model.py
11
model.py
@ -5,6 +5,13 @@ import os
|
||||
import datetime
|
||||
|
||||
|
||||
def generate_random_id(_s=set()):
|
||||
while (new_id := base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower()) in _s:
|
||||
pass
|
||||
_s.add(new_id)
|
||||
return new_id
|
||||
|
||||
|
||||
class Model:
|
||||
def __init__(self, filename = "tehmodel.json"):
|
||||
self.sockets = {} # mapping: client -> socket
|
||||
@ -48,13 +55,13 @@ class Model:
|
||||
return clientid in self.model["clients"]
|
||||
|
||||
def create_client(self) -> str:
|
||||
clientname = base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower()
|
||||
clientname = generate_random_id()
|
||||
newclient = {"id": clientname}
|
||||
self.model["clients"][clientname] = newclient
|
||||
return clientname
|
||||
|
||||
def create_session(self) -> str:
|
||||
sessionname = base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower()
|
||||
sessionname = generate_random_id()
|
||||
newsession = {"id": sessionname, "clients": []}
|
||||
self.model["sessions"][sessionname] = newsession
|
||||
return sessionname
|
||||
|
Loading…
Reference in New Issue
Block a user