-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (121 loc) · 5.51 KB
/
app.py
File metadata and controls
146 lines (121 loc) · 5.51 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask, render_template, request, redirect, url_for, session
from flask_socketio import SocketIO, emit, join_room, leave_room
from threading import Thread, Lock
import requests
import datetime
import pika
import re
app = Flask(__name__)
app.debug = True
app.secret_key = '35ba1b653d97423f9bd67d3309bd012b'
socketio = SocketIO(app, logger=True, engineio_logger=True)
#socketio.init_app(app)
### App Routes ###
@app.route('/')
@app.route('/login', methods =['GET', 'POST'])
def login():
from functions.sqlquery import sql_query2
msg = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
username = request.form['username']
password = request.form['password']
room = request.form['room']
cursor = sql_query2('SELECT * FROM data_table WHERE username = ? AND password = ?', (username, password))
if len(cursor) > 0:
account = cursor[0]
session['loggedin'] = True
session['room'] = room
session['username'] = account['username']
msg = 'Logged in successfully !'
return render_template('chat.html', name=account['username'], room=session['room'])
else:
msg = 'Incorrect username / password !'
return render_template('login.html', msg = msg)
@app.route('/logout')
def logout():
session.pop('loggedin', None)
session.pop('room', None)
session.pop('username', None)
return redirect(url_for('login'))
@app.route('/register', methods =['GET', 'POST'])
def register():
from functions.sqlquery import sql_query2, sql_edit_insert
msg = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
email = request.form['email']
username = request.form['username']
password = request.form['password']
cursor = sql_query2(''' SELECT * FROM data_table where username = ?''', (username,))
if len(cursor) > 0:
msg = 'Account already exists!'
elif not re.match(r'[A-Za-z0-9]+', username):
msg = 'Username must contain only characters and numbers!'
elif not re.search(r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$',email):
msg = 'Email must be a valid one!'
elif not username or not password or not email:
msg = 'Please fill out the form !'
else:
sql_edit_insert('INSERT INTO data_table(email,username,password) VALUES (?, ?, ?)', (email,username,password))
msg = 'You have successfully registered !'
elif request.method == 'POST':
msg = 'Please fill out the form !'
return render_template('register.html', msg = msg)
@app.route('/chat')
def chat():
"""Chat room. The user's name and room must be stored in
the session."""
name = session.get('username', '')
room = session.get('room', '')
if name == '' or room == '':
return redirect(url_for('login'))
return render_template('chat.html', name=name, room=room)
@app.route('/rmq-redirect', methods=['GET'])
def sendBotReply():
data = request.args.get('msg').split('|')
message = data[0]
room = data[1]
if message.startswith('ERR: '):
message = data[0].replace('ERR: ', '')
dtMsg = datetime.datetime.now()
curDate = '{}-{:02d}-{:02d}'.format(dtMsg.year, dtMsg.month, dtMsg.day)
curTime = '{:02d}:{:02d}:{:02d}'.format(dtMsg.hour, dtMsg.minute, dtMsg.second)
emit('message', {'msg': curDate + ' ' + curTime + ' | ' + 'StooqBot' + ': ' + message}, room=room, namespace='/chat')
return "Ok"
@socketio.on('joined', namespace='/chat')
def joined(message):
room = session.get('room')
join_room(room)
emit('status', {'msg': session.get('username') + ' has entered the room.'}, room=room)
@socketio.on('text', namespace='/chat')
def text(message):
room = session.get('room')
dtMsg = datetime.datetime.now()
curDate = '{}-{:02d}-{:02d}'.format(dtMsg.year, dtMsg.month, dtMsg.day)
curTime = '{:02d}:{:02d}:{:02d}'.format(dtMsg.hour, dtMsg.minute, dtMsg.second)
emit('message', {'msg': curDate + ' ' + curTime + ' | ' + session.get('username') + ': ' + message['msg']}, room=room)
match = re.search('(?<=\/)(.*?)(?=\=)', message['msg'])
if match:
cmd = message['msg'][match.start():match.end()]
ticker = match.string.split('=')[1]
sendMessageRabbitMQ('{}|{}|{}'.format(cmd, match.string.split('=')[1], room))
@socketio.on('left', namespace='/chat')
def left(message):
room = session.get('room')
leave_room(room)
emit('status', {'msg': session.get('username') + ' has left the room.'}, room=room)
def sendMessageRabbitMQ(message):
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials('user', 'password')))
channel = connection.channel()
channel.basic_publish(exchange='my_exchange', routing_key='test', body=message)
connection.close()
### Consumer RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
channel = connection.channel()
def callback(ch, method, properties, body):
requests.get('http://localhost:5000/rmq-redirect?msg=' + body.decode("utf-8"))
channel.basic_consume(queue="my_app", on_message_callback=callback, auto_ack=True)
thread = Thread(target = channel.start_consuming)
thread.start()
### Start API
if __name__ == "__main__":
socketio.run(app, port=5000)