43 lines
1.0 KiB
Python
43 lines
1.0 KiB
Python
#!/usr/bin/env python
|
|
|
|
import asyncio
|
|
import websockets
|
|
import json
|
|
import sys
|
|
import os
|
|
from routes import Routes
|
|
|
|
|
|
routes = Routes()
|
|
|
|
|
|
class UserInfoProtocol(websockets.BasicAuthWebSocketServerProtocol):
|
|
async def check_credentials(self, username, password):
|
|
all_users = routes.users()
|
|
self.user = all_users.get(username, None)
|
|
return self.user != None
|
|
|
|
|
|
async def handle(websocket):
|
|
try:
|
|
await routes.add_connection(websocket)
|
|
|
|
async for message in websocket:
|
|
event = json.loads(message)
|
|
print(event, file=sys.stderr)
|
|
await routes.call(event["action"], websocket, event)
|
|
|
|
finally:
|
|
await routes.remove_connection(websocket)
|
|
|
|
|
|
async def main():
|
|
port = os.environ.get("BEERLOG_PORT", 8000)
|
|
host = os.environ.get("BEERLOG_HOST", "0.0.0.0")
|
|
print(f"Start on {host}:{port}", file=sys.stderr)
|
|
async with websockets.serve(handle, host, port, create_protocol=UserInfoProtocol):
|
|
await asyncio.Future()
|
|
|
|
|
|
asyncio.run(main())
|