From 3e8669f09f6d90bc01ffdca8e66647a6d62085fe Mon Sep 17 00:00:00 2001 From: ericz Date: Sun, 25 Aug 2024 20:49:53 -0400 Subject: [PATCH] refactor server --- api-server/app.py | 92 +++++--------------------------------------- api-server/chat.py | 55 ++++++++++++++++++++++++++ api-server/webrtc.py | 17 ++++++++ 3 files changed, 82 insertions(+), 82 deletions(-) create mode 100644 api-server/chat.py create mode 100644 api-server/webrtc.py diff --git a/api-server/app.py b/api-server/app.py index 9da0fbc..6e6a88b 100644 --- a/api-server/app.py +++ b/api-server/app.py @@ -1,16 +1,16 @@ -from flask import Flask, jsonify, request -from flask_socketio import SocketIO, emit, join_room, leave_room +from flask import Flask +from flask_socketio import SocketIO from flask_cors import CORS -import uuid +from chat import setup_chat_routes, setup_chat_sockets +from webrtc import setup_webrtc_sockets -# Define log levels +# Define log levels and color codes DEBUG = 'DEBUG' INFO = 'INFO' WARNING = 'WARNING' ERROR = 'ERROR' CRITICAL = 'CRITICAL' -# Define color codes DEBUG_COLOR = '\033[94m' INFO_COLOR = '\033[92m' WARNING_COLOR = '\033[93m' @@ -38,82 +38,10 @@ def log_message(level, message, meeting=''): # In-memory storage for each session session_storage = {} -# Route to generate a unique meeting link -@app.route('/api/create-meeting', methods=['POST']) -def create_meeting(): - # Generate a unique meeting ID - meeting_id = str(uuid.uuid4()) - data = request.get_json() - username = data['username'] - session_storage[meeting_id] = { - 'host': username, - 'users': [], - 'chat_history': [] - } - log_message(INFO, f'User {username} created a new meeting', meeting_id) - return jsonify({'meeting_id': meeting_id}) - -# When users want to join a meeting -@socketio.on('join') -def handle_join(data): - if 'username' not in data or 'meeting_id' not in data: - log_message(ERROR, f'Join request missing username or meeting ID: {data}') - return - meeting_id = data['meeting_id'] - username = data['username'] - - if meeting_id not in session_storage: - emit('error', 'Invalid meeting ID', room=meeting_id) - return - session = session_storage[meeting_id] - if username in session_storage[meeting_id]['users']: - emit('error', 'User already joined', room=meeting_id) - return - session['users'].append(username) - - join_room(meeting_id) - log_message(INFO, f'User {username} joined the meeting', meeting_id) - emit('user_joined', {'username': username, 'meeting_id': meeting_id}, room=meeting_id) - -# Handle chat messages -@socketio.on('chat_message') -def handle_chat_message(data): - meeting_id = data['meeting_id'] - sender = data['sender'] - message = data['text'] - - # Save the chat message to the dictionary - if meeting_id not in session_storage: - return - - session_storage[meeting_id]['chat_history'].append({'sender': sender, 'text': message}) - - log_message(INFO, f'User {sender} sent: {message}', meeting_id) - emit('chat_message', {'sender': sender, 'text': message}, room=meeting_id) - -# Endpoint to retrieve chat history -@app.route('/api/chat_history/', methods=['GET']) -def get_chat_history(meeting_id): - if meeting_id in session_storage: - return jsonify(session_storage[meeting_id]['chat_history']) - else: - return jsonify({'error': 'Meeting ID not found'}), 404 - -# Handle SDP (Session Description Protocol) messages -@socketio.on('offer') -def handle_offer(data): - meeting_id = data['meeting_id'] - emit('offer', data, room=meeting_id) - -@socketio.on('answer') -def handle_answer(data): - meeting_id = data['meeting_id'] - emit('answer', data, room=meeting_id) - -@socketio.on('ice_candidate') -def handle_ice_candidate(data): - meeting_id = data['meeting_id'] - emit('ice_candidate', data, room=meeting_id) +# Setup routes and sockets +setup_chat_routes(app, session_storage, log_message) +setup_chat_sockets(socketio, session_storage, log_message) +setup_webrtc_sockets(socketio) if __name__ == '__main__': - socketio.run(app, debug=True) + socketio.run(app, debug=True) \ No newline at end of file diff --git a/api-server/chat.py b/api-server/chat.py new file mode 100644 index 0000000..3528633 --- /dev/null +++ b/api-server/chat.py @@ -0,0 +1,55 @@ +from flask import jsonify, request +from flask_socketio import emit, join_room +import uuid + +def setup_chat_routes(app, session_storage, log_message): + @app.route('/api/create-meeting', methods=['POST']) + def create_meeting(): + meeting_id = str(uuid.uuid4()) + data = request.get_json() + username = data['username'] + session_storage[meeting_id] = { + 'host': username, + 'users': [], + 'chat_history': [] + } + log_message('INFO', f'User {username} created a new meeting', meeting_id) + return jsonify({'meeting_id': meeting_id}) + + @app.route('/api/chat_history/', methods=['GET']) + def get_chat_history(meeting_id): + if meeting_id in session_storage: + return jsonify(session_storage[meeting_id]['chat_history']) + else: + return jsonify({'error': 'Meeting ID not found'}), 404 + +def setup_chat_sockets(socketio, session_storage, log_message): + @socketio.on('join') + def handle_join(data): + if 'username' not in data or 'meeting_id' not in data: + log_message('ERROR', f'Join request missing username or meeting ID: {data}') + return + meeting_id = data['meeting_id'] + username = data['username'] + if meeting_id not in session_storage: + emit('error', 'Invalid meeting ID', room=meeting_id) + return + session = session_storage[meeting_id] + if username in session_storage[meeting_id]['users']: + emit('error', 'User already joined', room=meeting_id) + return + session['users'].append(username) + join_room(meeting_id) + log_message('INFO', f'User {username} joined the meeting', meeting_id) + emit('user_joined', {'username': username, 'meeting_id': meeting_id}, room=meeting_id) + + @socketio.on('chat_message') + def handle_chat_message(data): + meeting_id = data['meeting_id'] + sender = data['sender'] + message = data['text'] + if meeting_id not in session_storage: + return + session_storage[meeting_id]['chat_history'].append({'sender': sender, 'text': message}) + log_message('INFO', f'User {sender} sent: {message}', meeting_id) + emit('chat_message', {'sender': sender, 'text': message}, room=meeting_id) \ No newline at end of file diff --git a/api-server/webrtc.py b/api-server/webrtc.py new file mode 100644 index 0000000..74c75a3 --- /dev/null +++ b/api-server/webrtc.py @@ -0,0 +1,17 @@ +from flask_socketio import emit + +def setup_webrtc_sockets(socketio): + @socketio.on('offer') + def handle_offer(data): + meeting_id = data['meeting_id'] + emit('offer', data, room=meeting_id) + + @socketio.on('answer') + def handle_answer(data): + meeting_id = data['meeting_id'] + emit('answer', data, room=meeting_id) + + @socketio.on('ice_candidate') + def handle_ice_candidate(data): + meeting_id = data['meeting_id'] + emit('ice_candidate', data, room=meeting_id) \ No newline at end of file