Add start.py to initialize logging and create database tables
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from dotenv import load_dotenv
|
||||
import db
|
||||
import os
|
||||
import logging
|
||||
|
||||
#Logging
|
||||
|
||||
logging.basicConfig(
|
||||
filename='app.log', # Log to a file
|
||||
level=logging.DEBUG, # Log everything (DEBUG and above)
|
||||
format='%(asctime)s %(levelname)s %(message)s'
|
||||
)
|
||||
|
||||
logging.info('Application started')
|
||||
try:
|
||||
logging.debug('Debug info here')
|
||||
except Exception as e:
|
||||
logging.exception('An error occurred')
|
||||
|
||||
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)
|
||||
|
||||
db.connect(db_user, db_psswd, db_host, int(db_port), db_name)
|
||||
|
||||
def new_table(name):
|
||||
cursor = db.connection.cursor()
|
||||
if name == "tokens":
|
||||
executed = "CREATE TABLE tokens (tokens VARCHAR(50) PRIMARY KEY, `time` VARCHAR(50), duration VARCHAR(50))"
|
||||
cursor.execute(executed)
|
||||
db.connection.commit()
|
||||
logging.info("Table 'tokens' created")
|
||||
|
||||
if name == "luggage":
|
||||
executed = "CREATE TABLE luggage (clothes VARCHAR(50) PRIMARY KEY, `weight` DECIMAL(20,2), pieces INT(11))"
|
||||
cursor.execute(executed)
|
||||
db.connection.commit()
|
||||
logging.info("Table 'luggage' created")
|
||||
|
||||
|
||||
new_table("tokens")
|
||||
new_table("luggage")
|
||||
|
||||
Reference in New Issue
Block a user