From 6ebf34c4a91eaf2f26c0551feb7bcc721913a6a9 Mon Sep 17 00:00:00 2001 From: arne Date: Tue, 31 Jan 2023 20:03:26 +0100 Subject: [PATCH] major cleanup. Sir, yes, sir! --- app.py | 85 ++++++---- app.sh | 2 +- docs/client.html | 56 ------ docs/cloud.html | 396 +++++++++++++++++++++++++++++++++++++++++++ docs/echo.html | 35 ---- docs/other.html | 56 ------ docs/receive.html | 43 ----- requirements.txt | 12 +- templates/base.html | 22 --- templates/index.html | 20 --- test.py | 57 ------- wsgi.py | 4 - 12 files changed, 448 insertions(+), 340 deletions(-) mode change 100755 => 100644 app.py delete mode 100644 docs/client.html create mode 100644 docs/cloud.html delete mode 100644 docs/echo.html delete mode 100644 docs/other.html delete mode 100644 docs/receive.html delete mode 100644 templates/base.html delete mode 100644 templates/index.html delete mode 100644 test.py delete mode 100644 wsgi.py diff --git a/app.py b/app.py old mode 100755 new mode 100644 index b5d5ddf..fbf2f5f --- a/app.py +++ b/app.py @@ -1,43 +1,58 @@ -#!/usr/bin/env python3 -from flask import Flask, render_template, url_for, request, redirect, Response, abort, session -from flask_sock import Sock -from flask_sqlalchemy import SQLAlchemy -from werkzeug.middleware.proxy_fix import ProxyFix -from datetime import datetime -import magic -import random -import string +import asyncio +import websockets import json -app = Flask(__name__) -app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1) -app.secret_key = '029c0gji3jfo3o8h938vhwtfmh3t39th' -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///cloudvars.db' -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False -db = SQLAlchemy(app) -sock = Sock(app) +clients = {} +vars = {} -class variables(db.Model): - id = db.Column(db.Integer, primary_key=True, autoincrement=True) - namespace = db.Column(db.String(64), nullable=False) - project = db.Column(db.String(64), nullable=False) - name = db.Column(db.String(64), nullable=False) - value = db.Column(db.String(64), nullable=True) +async def handshake(ws,data): + project_id = data.get('project_id','') + user = data.get('user') + print(f"{project_id} new user connected from {ws.host}:{ws.port}") + + if not clients.get(project_id): + clients[project_id] = [] + clients[project_id].append(ws) + await get_variables(ws,project_id,user) + return (project_id,user) -@app.context_processor -def get_date(): - date = datetime.now() - return { "now": date.strftime("%Y-%m-%d") } +async def get_variables(ws,project_id,user): + for name,value in vars.get(project_id,{}).items(): + if not name: + continue + await ws.send(json.dumps({"method":"set","name":name,"value":value})) -@sock.route('/') -def echo(sock): - while True: - data = json.loads(sock.receive()) - sock.send(json.dumps({"method":"set","name":"test","value":"abcd"})) +async def set_variable(project_id,var): + if not vars.get(project_id,''): + vars[project_id] = {} + vars[project_id][var.get('name','')] = var.get('value','') + await websockets.broadcast(clients.get(project_id,[]),json.dumps(var)) -@app.route('/', methods = ['GET']) -def index(): - return render_template('index.html') +async def disconnect(project_id,user): + print("disconnecting",user) + clients.get(project_id,[]).pop(user) + + +async def process(ws, path): + project_id = "" + user = "" + async for msg in ws: + print(f"< {user} - {msg}") + try: + data = json.loads(msg) + method = data.get('method','') + if method == 'handshake': + project_id,user = await handshake(ws,data) + if method in ['set','create']: + await set_variable(project_id,data) + finally: + pass + await disconnect(project_id,ws) if __name__ == "__main__": - app.run(debug=True) + try: + start_server = websockets.serve(process, 'localhost', 5000, compression=None) + finally: + pass + asyncio.get_event_loop().run_until_complete(start_server) + asyncio.get_event_loop().run_forever() \ No newline at end of file diff --git a/app.sh b/app.sh index 4265391..5590ec5 100755 --- a/app.sh +++ b/app.sh @@ -5,4 +5,4 @@ cd $BASE git pull source env/bin/activate pip3 install -r requirements.txt -gunicorn -D --bind 0.0.0.0:80 --pid /var/run/webapp.pid wsgi:app +python3 app.py \ No newline at end of file diff --git a/docs/client.html b/docs/client.html deleted file mode 100644 index 94b18d9..0000000 --- a/docs/client.html +++ /dev/null @@ -1,56 +0,0 @@ - - -
- - - - \ No newline at end of file diff --git a/docs/cloud.html b/docs/cloud.html new file mode 100644 index 0000000..c472f64 --- /dev/null +++ b/docs/cloud.html @@ -0,0 +1,396 @@ + + + + + + + + + cloud + + + + + +
+ + + +
+ + + +
+
+ + + + + + + + + + + diff --git a/docs/echo.html b/docs/echo.html deleted file mode 100644 index b0c20d4..0000000 --- a/docs/echo.html +++ /dev/null @@ -1,35 +0,0 @@ - - -
- - - - \ No newline at end of file diff --git a/docs/other.html b/docs/other.html deleted file mode 100644 index 6c104c3..0000000 --- a/docs/other.html +++ /dev/null @@ -1,56 +0,0 @@ - - -
- - - - \ No newline at end of file diff --git a/docs/receive.html b/docs/receive.html deleted file mode 100644 index f2e7462..0000000 --- a/docs/receive.html +++ /dev/null @@ -1,43 +0,0 @@ - - -
- - - - \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e9d24e9..7a38911 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1 @@ -python-magic -Click -Flask -Flask-SQLAlchemy -gunicorn -itsdangerous -Jinja2 -MarkupSafe -SQLAlchemy -Werkzeug -flask-sock \ No newline at end of file +websockets \ No newline at end of file diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index 2663fe5..0000000 --- a/templates/base.html +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - {% block head %}Scratch Cloudvars Server{% endblock %} - - -
- {% block header %}homeScratch Cloudvars Server{% endblock %} -
-
- {% block content %}{% endblock %} -
- - - diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index c2da5a2..0000000 --- a/templates/index.html +++ /dev/null @@ -1,20 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} -
- -
-{% endblock %} \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index 1ab7e6d..0000000 --- a/test.py +++ /dev/null @@ -1,57 +0,0 @@ -import asyncio -import websockets -import json - -clients = {} -vars = {} - -async def handshake(ws,j): - if not clients.get(j.get('project_id')): - clients[j.get('project_id')] = {} - clients[j.get('project_id')][j.get('user')] = ws - print(f"{j.get('user')} connected from {ws.host}:{ws.port}") - return(j.get('project_id'),j.get('user')) - -async def get_variables(websocket,project_id,user): - for name,value in vars.get(project_id,{}).items(): - if not name: - continue - await websocket.send(json.dumps({"method":"set","name":name,"value":value})) - -async def set_variable(project_id,user,var): - if not vars.get(project_id,''): - vars[project_id] = {} - vars[project_id][var.get('name','')] = var.get('value','') - - data = json.dumps(var) - for name,ws in clients.get(project_id,{}).items(): - if name == user: - continue - print(f"> {name} - {data}") - await ws.send(data) - -async def disconnect(project_id,user): - print("disconnecting",user) - clients.get(project_id,[]).pop(user) - - -async def process(websocket, path): - project_id = "" - user = "" - async for msg in websocket: - print(f"< {user} - {msg}") - try: - data = json.loads(msg) - method = data.get('method','') - if method == 'handshake': - project_id,user = await handshake(websocket,data) - await get_variables(websocket,project_id,user) - if method in ['set','create']: - await set_variable(project_id,user,data) - finally: - pass - await disconnect(project_id,user) - -start_server = websockets.serve(process, 'localhost', 5000, compression=None) -asyncio.get_event_loop().run_until_complete(start_server) -asyncio.get_event_loop().run_forever() \ No newline at end of file diff --git a/wsgi.py b/wsgi.py deleted file mode 100644 index 6026b0f..0000000 --- a/wsgi.py +++ /dev/null @@ -1,4 +0,0 @@ -from app import app - -if __name__ == "__main__": - app.run()