From 4c787e8f9513614e13f037d05083199885bc8329 Mon Sep 17 00:00:00 2001 From: Joem Elias Sanez <61286823+israelias@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:38:59 +0200 Subject: [PATCH] Refactor backend connection --- backend/.python-version | 1 + backend/app.py | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 backend/.python-version diff --git a/backend/.python-version b/backend/.python-version new file mode 100644 index 0000000..8e34c81 --- /dev/null +++ b/backend/.python-version @@ -0,0 +1 @@ +3.9.19 diff --git a/backend/app.py b/backend/app.py index 48d897f..3ad7e93 100644 --- a/backend/app.py +++ b/backend/app.py @@ -22,10 +22,12 @@ from resources.listing import ListingAPI, ListingsAPI, listing_ns, listings_ns from resources.user import UserAPI, UsersAPI, user_ns, users_ns + + # Heroku-specific postgresql config -uri = os.getenv("DATABASE_URL") -if uri.startswith("postgres://"): - uri = uri.replace("postgres://", "postgresql://", 1) +# uri = os.getenv("DATABASE_URL") +# if uri and uri.startswith("postgres://"): +# uri = uri.replace("postgres://", "postgresql://", 1) if not os.path.exists("env.py"): @@ -33,6 +35,8 @@ else: import env +debug = os.environ.get("DEVELOPMENT") + # =========================================================================== # * `Flask App and Configs` # ? Executes Flask app deployment @@ -42,7 +46,7 @@ # Initialize `app` app = Flask(__name__) -# basedir = os.path.abspath(os.path.dirname(__file__)) +basedir = os.path.abspath(os.path.dirname(__file__)) # Initialize `Blueprint API` @@ -52,11 +56,13 @@ # Apply database configs -# Development DB -# app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "db.sqlite") - -# Production DB -app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_DATABASE_URI") +if debug: + # Development DB + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "db.sqlite") +else: + # Production DB + app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL") +# Apply db settings app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["PROPAGATE_EXCEPTIONS"] = True