Files
  • main.py
  • chat
  • static
  • templates
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from gevent.monkey import patch_all; patch_all()

from chat.member import Member
from chat.server import SocketioBackend
from flask import Flask, render_template, request, redirect, make_response
from flask_socketio import SocketIO


app = Flask(
    __name__,
    static_folder="static",
    template_folder="templates"
)
socketio = SocketIO(app, logger=True)
chat_room = SocketioBackend()
socketio.on_namespace(chat_room)

MORSE_CODE = {
    'A':'.-', 'B':'-...',
    'C':'-.-.', 'D':'-..', 'E':'.',
    'F':'..-.', 'G':'--.', 'H':'....',
    'I':'..', 'J':'.---', 'K':'-.-',
    'L':'.-..', 'M':'--', 'N':'-.',
    'O':'---', 'P':'.--.', 'Q':'--.-',
    'R':'.-.', 'S':'...', 'T':'-',
    'U':'..-', 'V':'...-', 'W':'.--',
    'X':'-..-', 'Y':'-.--', 'Z':'--..',
    '1':'.----', '2':'..---', '3':'...--',
    '4':'....-', '5':'.....', '6':'-....',
    '7':'--...', '8':'---..', '9':'----.',
    '0':'-----', ', ':'--..--', '.':'.-.-.-',
    '?':'..--..', '/':'-..-.', '-':'-....-',
    '(':'-.--.', ')':'-.--.-'
}

@app.route("/")
def index():
    return render_template("index.html", uses_script=False)

@app.route("/new_user", methods=["POST"])
def add_user():
    member = Member(request.form["username"])
    resp = make_response(redirect("/chat"))
    resp.set_cookie("member-id", member.private_id)
    chat_room.add_member(member)
    return resp

@app.route("/chat")
def chat():
    return render_template("chat.html", uses_script=True, sheet=MORSE_CODE)

if __name__ == "__main__":
    socketio.run(app, "0.0.0.0", 8080)