diff --git a/app.py b/app.py index 7cf78df..bdc1360 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,13 @@ #!/usr/bin/env python3 -from flask import Flask, render_template, request, redirect, url_for, make_response -from dotenv import load_dotenv import os +import datetime +from flask import Flask, render_template, request, redirect, url_for +# from markupsafe import escape import pymongo -import datetime from bson.objectid import ObjectId -import sys +from dotenv import load_dotenv # instantiate the app app = Flask(__name__) @@ -16,27 +16,27 @@ # if you do not yet have a file named .env, make one based on the template in env.example load_dotenv() # take environment variables from .env. -# turn on debugging if in development mode -if os.getenv("FLASK_ENV", "development") == "development": - # turn on debugging, if in development - app.debug = True # debug mnode +# # turn on debugging if in development mode +# if os.getenv("FLASK_ENV", "development") == "development": +# # turn on debugging, if in development +# app.debug = True # debug mnode # connect to the database -cxn = pymongo.MongoClient(os.getenv("MONGO_URI"), serverSelectionTimeoutMS=5000) +cxn = pymongo.MongoClient(os.getenv("MONGO_URI")) +db = cxn[os.getenv("MONGO_DBNAME")] # store a reference to the database + +# the following try/except block is a way to verify that the database connection is alive (or not) try: # verify the connection works by pinging the database cxn.admin.command("ping") # The ping command is cheap and does not require auth. - db = cxn[os.getenv("MONGO_DBNAME")] # store a reference to the database print(" *", "Connected to MongoDB!") # if we get here, the connection worked! except Exception as e: # the ping command failed, so the connection is not available. - print(" *", "Failed to connect to MongoDB at", os.getenv("MONGO_URI")) - print("Database connection error:", e) # debug + print(" * MongoDB connection error:", e) # debug # set up the routes -# route for the home page @app.route("/") def home(): """ @@ -126,10 +126,9 @@ def handle_error(e): # run the app if __name__ == "__main__": - PORT = os.getenv( - "PORT", 5000 - ) # use the PORT environment variable, or default to 5000 + # use the PORT environment variable, or default to 5000 + FLASK_PORT = os.getenv("FLASK_PORT", "5000") # import logging # logging.basicConfig(filename='/home/ak8257/error.log',level=logging.DEBUG) - app.run(port=PORT) + app.run(port=FLASK_PORT, debug=True) diff --git a/env.example b/env.example index adebd4a..8018d6b 100644 --- a/env.example +++ b/env.example @@ -2,5 +2,6 @@ MONGO_DBNAME=your_db_name MONGO_URI="mongodb://your_db_username:your_db_password@your_db_host_server_name:27017/your_db_name?authSource=admin&retryWrites=true&w=majority" FLASK_APP=app.py FLASK_ENV=development -GITHUB_SECRET=your_github_secret -GITHUB_REPO=https://github.com/your-repository-url +FLASK_PORT=5000 +GITHUB_REPO=https://github.com/your-repository-url # unnecessary for basic operation +GITHUB_SECRET=your_github_secret # unnecessary for basic operation