From 2ad4976c646da1afbe111232a28f5169542380cc Mon Sep 17 00:00:00 2001 From: shriyakalakata Date: Fri, 23 Feb 2024 18:44:11 -0500 Subject: [PATCH 1/2] Set up db connection in app --- app.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..accc618 --- /dev/null +++ b/app.py @@ -0,0 +1,23 @@ +from flask import Flask, render_template, request, redirect, abort, url_for, make_response +from dotenv import load_dotenv +import os +import pymongo +from pymongo import MongoClient + +load_dotenv() + +app = Flask(__name__) + +uri = os.getenv("MONGO_URI") +db_name = os.getenv("MONGO_DBNAME") + +# Create a new client and connect to the server +client = MongoClient(uri) +db = client[db_name] + +# Send a ping to confirm a successful connection +try: + client.admin.command('ping') + print("Connected to MongoDB!") +except Exception as e: + print("MongoDB connection error:", e) \ No newline at end of file From 195d9f20775cd0702e9c101cb9c7624106bc97ee Mon Sep 17 00:00:00 2001 From: shriyakalakata Date: Fri, 23 Feb 2024 18:53:38 -0500 Subject: [PATCH 2/2] Add code to run the app --- app.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index accc618..c0d0534 100644 --- a/app.py +++ b/app.py @@ -20,4 +20,10 @@ client.admin.command('ping') print("Connected to MongoDB!") except Exception as e: - print("MongoDB connection error:", e) \ No newline at end of file + print("MongoDB connection error:", e) + + +# run the app +if __name__ == "__main__": + FLASK_PORT = os.getenv("FLASK_PORT", "5000") + app.run(port=FLASK_PORT) \ No newline at end of file