-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
92 lines (80 loc) · 2.12 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
from flask import Flask, render_template, request, redirect, url_for
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
chats = [
{
"messages": [
{
"user": "Bharat",
"message": "Hello Everyone",
},
{
"user": "Arjun",
"message": "Hello Bharat",
}
]
},
{
"messages": [
{
"user": "Bharat",
"message": "Hello Everyone",
},
{
"user": "Arjun",
"message": "Hello Bharat",
}
]
},
]
channels = [
{
"id": 0,
"created_by": "Bharat",
"name": "Forza Juve",
"desc": "A chat room for Juve fans"
},
{
"id": 1,
"created_by": "Arjun",
"name": "Anime Addicts",
"desc": "Only Anime addicts allowed!"
}
]
@app.route("/")
def index():
return render_template("index.html", channels=channels)
@app.route("/add", methods=["GET", "POST"])
def add():
if request.method == "POST":
username = request.form.get("username")
channelname = request.form.get("channelname")
desc = request.form.get("desc")
print(f"{username} {channelname} {desc}")
channels.append({
"id": len(channels),
"created_by": username,
"name": channelname,
"desc": desc,
})
chats.append({
"messages": []
})
return redirect(url_for('index'))
return render_template("add.html")
@app.route("/chat/<int:channel_id>")
def chat(channel_id):
channel = channels[channel_id]
messages = chats[channel_id]["messages"]
return render_template("chat.html", channel=channel, messages=messages)
@socketio.on("send msg")
def msg(data):
print(data["message"])
chats[int(data["id"])]["messages"].append({
"user": data["name"],
"message": data["message"]
})
emit("update message", data, broadcast=True)