diff --git a/__pycache__/main.cpython-311.pyc b/__pycache__/main.cpython-311.pyc new file mode 100644 index 0000000..eabb62e Binary files /dev/null and b/__pycache__/main.cpython-311.pyc differ diff --git a/handlers/__pycache__/http.cpython-311.pyc b/handlers/__pycache__/http.cpython-311.pyc new file mode 100644 index 0000000..956b949 Binary files /dev/null and b/handlers/__pycache__/http.cpython-311.pyc differ diff --git a/handlers/__pycache__/socketio.cpython-311.pyc b/handlers/__pycache__/socketio.cpython-311.pyc new file mode 100644 index 0000000..74f21a2 Binary files /dev/null and b/handlers/__pycache__/socketio.cpython-311.pyc differ diff --git a/handlers/http.py b/handlers/http.py new file mode 100644 index 0000000..afc3fb2 --- /dev/null +++ b/handlers/http.py @@ -0,0 +1,28 @@ +from flask import request, jsonify +from misc.random_string import generate_secure_string + + +def init_http_handlers(app): + @app.route('/') + def index(): + return "Добро пожаловать на Flask сервер!" + + @app.route('/api/new-request', methods=['POST']) + def handle_data(): + # Получаем JSON из запроса + data = request.get_json() + + if not data: + return jsonify({"error": "No JSON payload received"}), 400 + + # Выводим полученные данные в консоль + print("Полученные данные:", data) + + # Формируем ответ + response = { + "message": "Data received successfully", + "yourData": data, + "conversation_id":generate_secure_string(40) + } + + return jsonify(response), 200 diff --git a/handlers/socketio.py b/handlers/socketio.py new file mode 100644 index 0000000..bd03008 --- /dev/null +++ b/handlers/socketio.py @@ -0,0 +1,25 @@ + +from misc.conversations import Conversations +from flask import Flask, request +from flask_socketio import join_room, leave_room + +def init_socketio_handlers(socketio): + @socketio.on('connect') + def handle_connect(): + print('Клиент подключился') + ip_address = request.remote_addr # Получение IP-адреса клиента + sid = request.sid + print(f'Клиент подключился с IP: {ip_address} SID: {sid}') + + @socketio.on('disconnect') + def handle_disconnect(): + print('Клиент отключился') + + @socketio.on('new_problem') + def handle_message(data): + print('Получено сообщение:', data, ip_address) + conversation = Conversations().create(data.get("message"),data.get("work_place"),data.get("name"),data.get("problem")) + + + socketio.emit('response', {'data': conversation}) + print(data) \ No newline at end of file diff --git a/main.py b/main.py index c3fa03e..25a7b95 100644 --- a/main.py +++ b/main.py @@ -1,32 +1,14 @@ from flask import Flask, request, jsonify -from misc.random_string import generate_secure_string +from flask_socketio import SocketIO, emit +from handlers.http import init_http_handlers +from handlers.socketio import init_socketio_handlers + app = Flask(__name__) - -@app.route('/') -def index(): - return "Добро пожаловать на Flask сервер!" - -@app.route('/api/new-request', methods=['POST']) -def handle_data(): - # Получаем JSON из запроса - data = request.get_json() - - if not data: - return jsonify({"error": "No JSON payload received"}), 400 - - # Выводим полученные данные в консоль - print("Полученные данные:", data) - - # Формируем ответ - response = { - "message": "Data received successfully", - "yourData": data, - "conversation_id":generate_secure_string(40) - } - - return jsonify(response), 200 +socketio = SocketIO(app,cors_allowed_origins="*") +init_socketio_handlers(socketio) +init_http_handlers(app) if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + socketio.run(app, debug=True) \ No newline at end of file diff --git a/misc/__pycache__/conversations.cpython-311.pyc b/misc/__pycache__/conversations.cpython-311.pyc new file mode 100644 index 0000000..2528187 Binary files /dev/null and b/misc/__pycache__/conversations.cpython-311.pyc differ diff --git a/misc/__pycache__/random_string.cpython-311.pyc b/misc/__pycache__/random_string.cpython-311.pyc new file mode 100644 index 0000000..e152eed Binary files /dev/null and b/misc/__pycache__/random_string.cpython-311.pyc differ diff --git a/misc/conversations.py b/misc/conversations.py index e33412d..68ad8c2 100644 --- a/misc/conversations.py +++ b/misc/conversations.py @@ -1,4 +1,4 @@ -from random_string import generate_secure_string +from misc.random_string import generate_secure_string CONVERSATIONS = {} """ @@ -29,5 +29,5 @@ class Conversations: "workspace":workspace, "user_name":user_name, "problem_type":problem_type} - return CONVERSATIONS[self.id] + return {"conversation":CONVERSATIONS[self.id], "id":self.id}