Skip to content

Commit

Permalink
dependencies and env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bloombar committed Nov 25, 2023
1 parent 7fe7007 commit 155ae9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
33 changes: 16 additions & 17 deletions app.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand All @@ -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():
"""
Expand Down Expand Up @@ -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)
5 changes: 3 additions & 2 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 155ae9d

Please sign in to comment.