From 02ab93fc532a3e1b3aa249008c9d00f3ab461230 Mon Sep 17 00:00:00 2001 From: null Date: Wed, 28 Feb 2024 21:44:33 +0100 Subject: [PATCH] reconnect after disconnect --- __init__.py | 0 app.py | 16 ++++------------ db.py | 21 +++++++++++++++++++++ routes/conditions.py | 26 ++++++++++++++------------ 4 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 __init__.py create mode 100644 db.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py index 9d32982..7286161 100644 --- a/app.py +++ b/app.py @@ -1,23 +1,15 @@ -import os from flask import Flask, render_template, jsonify -from rethinkdb import RethinkDB from routes.conditions import create_conditions_routes - -rethinkUrl = os.environ.get('RETHINKDB_URL') -rethinkPort = os.environ.get('RETHINKDB_PORT') +from db import get_connection, getRethinkDB app = Flask(__name__) +r = getRethinkDB() -# Connect to RethinkDB -r = RethinkDB() -conn = r.connect(rethinkUrl, rethinkPort, db='finfree') - -create_conditions_routes(app, r, conn) - +create_conditions_routes(app) @app.route('/') def index(): - cursor = r.table("conditions").run(conn) + cursor = r.table("conditions").run(get_connection()) conditions = list(cursor) return render_template('index.html', data={"conditions": conditions }) diff --git a/db.py b/db.py new file mode 100644 index 0000000..f920085 --- /dev/null +++ b/db.py @@ -0,0 +1,21 @@ +import os +from rethinkdb import RethinkDB + +rethinkUrl = os.environ.get('RETHINKDB_URL') +rethinkPort = os.environ.get('RETHINKDB_PORT') +rethinkdb = "finfree" + +r = RethinkDB() + +connection = None + +def get_connection(): + global connection + if connection is None or not connection.is_open(): + connection = r.connect(rethinkUrl, rethinkPort, db=rethinkdb) + + return connection + +def getRethinkDB(): + global r + return r \ No newline at end of file diff --git a/routes/conditions.py b/routes/conditions.py index eeac343..ed6f48c 100644 --- a/routes/conditions.py +++ b/routes/conditions.py @@ -1,20 +1,21 @@ from flask import jsonify, request, redirect, render_template -import rethinkdb as r +from db import get_connection, getRethinkDB table_name = 'conditions' route = table_name +r = getRethinkDB() -def create_conditions_routes(app, r, conn): +def create_conditions_routes(app): # Create a table (if not exists) - if table_name not in r.table_list().run(conn): - r.table_create(table_name).run(conn) + if table_name not in r.table_list().run(get_connection()): + r.table_create(table_name).run(get_connection()) # Routes for CRUD operations # Create operation @app.route('/' + route + '/create', methods=['GET']) def create_form(): - return render_template('conditions/create.html') + return render_template(route + '/create.html') @app.route('/' + route, methods=['POST']) def create_condition(): @@ -22,24 +23,25 @@ def create_conditions_routes(app, r, conn): data = request.json else: # Assuming form data is in 'application/x-www-form-urlencoded' format data = request.form.to_dict() - result = r.table(table_name).insert(data).run(conn) + + result = r.table(table_name).insert(data).run(get_connection()) return redirect(request.referrer or url_for('index')) # Read operation @app.route('/' + route, methods=['GET']) def get_conditions(): - cursor = r.table(table_name).run(conn) + cursor = r.table(table_name).run(get_connection()) conditions = list(cursor) return jsonify(conditions) @app.route('/' + route + '/', methods=['GET']) def get_condition(id): - cursor = r.table(table_name).get(id).run(conn) + cursor = r.table(table_name).get(id).run(get_connection()) json = jsonify(cursor) if request.headers.get('Content-Type') == 'application/json': return json else: - return render_template('conditions/update.html', data=cursor) + return render_template(route + '/update.html', data=cursor) # Update operation @app.route('/' + route + '/', methods=['POST']) @@ -48,16 +50,16 @@ def create_conditions_routes(app, r, conn): data = request.json else: data = request.form.to_dict() - r.table(table_name).get(id).update(data).run(conn) + r.table(table_name).get(id).update(data).run(get_connection()) return "" # Delete operation @app.route('/' + route + '/delete/', methods=['POST']) def delete_post(id): - r.table(table_name).get(id).delete().run(conn) + r.table(table_name).get(id).delete().run(get_connection()) return jsonify({'message': 'condition deleted successfully'}) @app.route('/' + route + '/', methods=['DELETE']) def delete_condition(id): - r.table(table_name).get(id).delete().run(conn) + r.table(table_name).get(id).delete().run(get_connection()) return jsonify({'message': 'condition deleted successfully'})