37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
import os
|
|
import json
|
|
import shutil
|
|
import mysql.connector
|
|
import bcrypt
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Global variables that is None by default and gets overwritten in other modules
|
|
|
|
app = None # main.py -> Flask App
|
|
sql_conn = None
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Global variables that initializes on first load of module
|
|
|
|
if not os.path.isfile("config.json"):
|
|
print("`config.json` were not found! Copying default_config and continuing...")
|
|
shutil.copy("default_config.json", "config.json")
|
|
|
|
with open("config.json", "r") as f:
|
|
config = json.load(f)
|
|
|
|
def make_sql_connection():
|
|
return mysql.connector.connect(**config["mysql"])
|
|
|
|
def get_sql_connection():
|
|
global sql_conn
|
|
if sql_conn is None or not sql_conn.is_connected():
|
|
sql_conn = make_sql_connection()
|
|
return sql_conn
|
|
|
|
def hash_password(password):
|
|
return bcrypt.hashpw(password.encode(), bcrypt.gensalt(10, prefix=b"2a")).decode()
|
|
|
|
def check_password(p1, p2):
|
|
return bcrypt.checkpw(p1.encode(), p2.encode())
|