add a simple async message capability

This commit is contained in:
randomuser 2023-05-24 09:27:35 -05:00
parent a292136b87
commit d597f5d4fc
3 changed files with 43 additions and 0 deletions

View File

@ -5,21 +5,51 @@ please see the terms of the license in the file LICENSE.
copyright 2023 randomuser
"""
import os
import hashlib
from flask import Flask
from flask import send_from_directory
from flask import redirect
from flask import request
from flask import render_template
from flask import escape
from flask_sock import Sock
import simple_websocket
import time
app = Flask(__name__, instance_relative_config=True)
sock = Sock(app)
clients = []
messages = {}
@app.route('/')
def index():
return redirect("/static/app")
@app.route('/messages')
def message_index():
argument = request.args.get('id')
if not argument:
return redirect("/static/messages.html")
else:
try:
text = messages[argument]
messages.pop(argument, None)
return text
except KeyError:
return redirect("/static/messages.html")
@app.route('/newmessage', methods = ['POST'])
def make_message():
if request.method == 'POST':
message = request.form['message']
hashobj = hashlib.sha256()
hashobj.update(bytes(message + str(time.time_ns()), 'utf-8'))
hashed = hashobj.hexdigest()
messages[hashed] = escape(message)
return render_template('messages.html', proto="http", host="127.0.0.1:5000", hashed=hashed)
@app.route('/static/app')
def app_redirect():
return redirect("/static/app.html")

View File

@ -0,0 +1,8 @@
<html>
<body>
<form action="http://localhost:5000/newmessage" method="post">
<input type="text" name="message" />
<input type="submit" value="submit" />
</form>
</body>
</html>

View File

@ -0,0 +1,5 @@
<!DOCTYPE html>
<title>Message Layout</title>
<body>
<p>Your generated url is <div id="finalurl">{{proto}}://{{host}}/messages?id={{hashed}}</div></p>
</body>