diff --git a/.env b/.env new file mode 100644 index 0000000..c37007c --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +#database +DB_NAME="canada_luggage" +#Credentials +DB_PASSWORD="13579,QsC" +DB_USER="luggage" +#Host Settings +DB_PORT="3307" +DB_HOST="192.168.1.74" \ No newline at end of file diff --git a/db.py b/db.py index 9a02e6c..7f528ac 100644 --- a/db.py +++ b/db.py @@ -2,7 +2,6 @@ import mariadb import logging - #Logging logging.basicConfig( @@ -18,14 +17,14 @@ except Exception as e: logging.exception('An error occurred') -def connect(): +def connect(db_user, db_psswd, db_host, db_port, db_name): try: global connection - connection = mariadb.connect(user="luggage", - password="13579,QsC", - host="192.168.1.74", - port=3307, - database="canada_luggage") + connection = mariadb.connect(user=db_user, + password=db_psswd, + host=db_host, + port=db_port, + database=db_name) print("Connected to MariaDB successfully!") logging.info("Connected to MariaDB successfully!") diff --git a/main.py b/main.py index 94a07ec..1772342 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,24 @@ import logging import time import os import sys +from dotenv import load_dotenv +import os + +#using .env +load_dotenv() +db_name = os.getenv("DB_NAME") +db_psswd = os.getenv("DB_PASSWORD") +db_user = os.getenv("DB_USER") +db_port = os.getenv("DB_PORT") or "3306" +db_host = os.getenv("DB_HOST") +if db_host == None: + logging.error("no hostname provided") + os._exit(1) + print("loading") open("app.log", "w").close() -db.connect() +db.connect(db_user, db_psswd, db_host, int(db_port), db_name) #Logging logging.basicConfig( filename='app.log', # Log to a file diff --git a/requirements.txt b/requirements.txt index d4af85e..9f55af5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ mariadb == 1.1.14 +python-dotenv == 1.2.3 \ No newline at end of file