-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
75 lines (59 loc) · 2.31 KB
/
application.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import logging
from flask import Flask, render_template, request, session, \
make_response, jsonify, redirect
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
# This is the structure that will hold all the channels and the messages in
# each channel.
# - A dictionary, each key represents a channel.
# - In each channel there is a list of dictionaries, each one is a message.
# - A message has two keys 'message' and 'dispalyname'
# Example: {'channel_1':
# [
# {'message': "Hello there!",
# 'display_name': "Kareem"},
# {'message': "Oh Hi!",
# 'display_name': "Shreef"},
# ]
# }
channels = dict()
@app.route("/", methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return signin()
else:
return render_template('index.html')
@app.route("/load", methods=['GET'])
def load():
logging.warning("the list of channels: " + str(channels))
logging.warning("json " + str(jsonify([channels])))
res = make_response(jsonify([list(channels.keys())]), 200)
return res
@app.route("/signin", methods=['POST', 'GET'])
def signin():
if request.method == 'POST':
return render_template('index.html')
else:
return render_template('signin.html')
@app.route("/create/<string:channel_name>", methods=['GET'])
def create(channel_name):
channels[channel_name] = []
logging.warning("Current channels " + str(channels))
return ''
@app.route("/chat/<string:channel_name>", methods=['GET'])
def chat(channel_name):
res = {'channel_name': channel_name,
'messages': channels[channel_name]}
logging.warning("channel data: " + str(res))
return render_template('chat.html', channel_data=res)
@socketio.on("message")
def message(message_data):
channels[message_data['channel_name']].\
append([{'message': message_data['message'],
'displayname': message_data['displayname']}])
logging.warning("Messages to be emitted: " + str(channels[message_data['channel_name']]))
emit('messages', {'messages': channels[message_data['channel_name']]},
broadcast=True)