import flask import flask_cors import uuid rooms = {} app = flask.Flask(__name__) flask_cors.CORS(app) @app.route('/') def index(): return flask.render_template('index.html') @app.route('/createRoom', methods=['POST']) def create_room(): roomId = flask.request.get_json()['roomIdInput'] success = True for room in rooms: if room == roomId: success = False if success: rooms.update({roomId: {}}) # Create an empty room with an empty message list return flask.jsonify({"success": True}) else: return flask.jsonify({"success": False}) @app.route('/postMessage', methods=['POST']) def post(): nameIn = flask.request.get_json()['nameInput'] messageIn = flask.request.get_json()['messageInput'] roomidIn = flask.request.get_json()['roomIdInput'] n = len(rooms[roomidIn])+1 rooms[roomidIn].update({"message"+str(n): {"name": nameIn, "message": messageIn}}) return flask.jsonify({"success": True}) @app.route('/messages', methods=['POST']) def messages(): print(len(rooms)) roomidInput = flask.request.get_json()['roomIdInput'] if roomidInput in rooms: return flask.jsonify(rooms[roomidInput]) else: return flask.jsonify({"message": "room does not exist"}) if __name__ == "__main__": app.run(debug=True,host="0.0.0.0",port=5000)