mirror of
https://github.com/PhoenixTwoFive/karaoqueue.git
synced 2025-05-18 18:41:48 +02:00
Create new config with credentials from env vars
This commit is contained in:
parent
12207c1246
commit
7ef938a5ff
4
.env.dev
4
.env.dev
@ -7,4 +7,6 @@ MARIADB_PASSWORD=mariadb_karaoqueue_password
|
|||||||
|
|
||||||
# Karaoqueue
|
# Karaoqueue
|
||||||
DEPLOYMENT_PLATFORM=Docker
|
DEPLOYMENT_PLATFORM=Docker
|
||||||
DBSTRING="mysql+pymysql://karaoqueue:mariadb_karaoqueue_password@127.0.0.1:3306/karaoqueue?charset=utf8mb4"
|
DBSTRING="mysql+pymysql://karaoqueue:mariadb_karaoqueue_password@127.0.0.1:3306/karaoqueue?charset=utf8mb4"
|
||||||
|
INITIAL_USERNAME=admin
|
||||||
|
INITIAL_PASSWORD=changeme
|
@ -11,6 +11,7 @@ import database
|
|||||||
data_directory = "data"
|
data_directory = "data"
|
||||||
config_file = data_directory+"/config.json"
|
config_file = data_directory+"/config.json"
|
||||||
|
|
||||||
|
|
||||||
def create_data_directory():
|
def create_data_directory():
|
||||||
if not os.path.exists(data_directory):
|
if not os.path.exists(data_directory):
|
||||||
os.makedirs(data_directory)
|
os.makedirs(data_directory)
|
||||||
@ -19,13 +20,16 @@ def create_data_directory():
|
|||||||
def get_catalog_url():
|
def get_catalog_url():
|
||||||
r = requests.get('https://www.karafun.de/karaoke-song-list.html')
|
r = requests.get('https://www.karafun.de/karaoke-song-list.html')
|
||||||
soup = BeautifulSoup(r.content, 'html.parser')
|
soup = BeautifulSoup(r.content, 'html.parser')
|
||||||
url = soup.findAll('a', href=True, text='Verfügbar in CSV-Format')[0]['href']
|
url = soup.findAll(
|
||||||
|
'a', href=True, text='Verfügbar in CSV-Format')[0]['href']
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
|
||||||
def get_songs(url):
|
def get_songs(url):
|
||||||
r = requests.get(url)
|
r = requests.get(url)
|
||||||
return r.text
|
return r.text
|
||||||
|
|
||||||
|
|
||||||
def is_valid_uuid(val):
|
def is_valid_uuid(val):
|
||||||
try:
|
try:
|
||||||
uuid.UUID(str(val))
|
uuid.UUID(str(val))
|
||||||
@ -33,12 +37,15 @@ def is_valid_uuid(val):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def check_config_exists():
|
def check_config_exists():
|
||||||
return database.check_config_table()
|
return database.check_config_table()
|
||||||
|
|
||||||
|
|
||||||
def load_version(app: Flask):
|
def load_version(app: Flask):
|
||||||
if os.environ.get("SOURCE_VERSION"):
|
if os.environ.get("SOURCE_VERSION"):
|
||||||
app.config['VERSION'] = os.environ.get("SOURCE_VERSION")[0:7] # type: ignore
|
app.config['VERSION'] = os.environ.get("SOURCE_VERSION")[
|
||||||
|
0:7] # type: ignore
|
||||||
elif os.path.isfile(".version"):
|
elif os.path.isfile(".version"):
|
||||||
with open('.version', 'r') as file:
|
with open('.version', 'r') as file:
|
||||||
data = file.read().replace('\n', '')
|
data = file.read().replace('\n', '')
|
||||||
@ -48,21 +55,22 @@ def load_version(app: Flask):
|
|||||||
app.config['VERSION'] = ""
|
app.config['VERSION'] = ""
|
||||||
else:
|
else:
|
||||||
app.config['VERSION'] = ""
|
app.config['VERSION'] = ""
|
||||||
|
|
||||||
|
|
||||||
def load_dbconfig(app: Flask):
|
def load_dbconfig(app: Flask):
|
||||||
if os.environ.get("FLASK_ENV") == "development":
|
if os.environ.get("FLASK_ENV") == "development":
|
||||||
app.config['DBCONNSTRING'] = os.environ.get("DBSTRING")
|
app.config['DBCONNSTRING'] = os.environ.get("DBSTRING")
|
||||||
else:
|
else:
|
||||||
if os.environ.get("DEPLOYMENT_PLATFORM") == "Heroku":
|
if os.environ.get("DEPLOYMENT_PLATFORM") == "Heroku":
|
||||||
if os.environ.get("JAWSDB_MARIA_URL"):
|
if os.environ.get("JAWSDB_MARIA_URL"):
|
||||||
app.config['DBCONNSTRING'] = os.environ.get("JAWSDB_MARIA_URL")
|
app.config['DBCONNSTRING'] = os.environ.get("JAWSDB_MARIA_URL")
|
||||||
else:
|
else:
|
||||||
app.config['DBCONNSTRING'] = ""
|
app.config['DBCONNSTRING'] = ""
|
||||||
if os.environ.get("DEPLOYMENT_PLATFORM") == "Docker":
|
if os.environ.get("DEPLOYMENT_PLATFORM") == "Docker":
|
||||||
if os.environ.get("DBSTRING"):
|
if os.environ.get("DBSTRING"):
|
||||||
app.config['DBCONNSTRING'] = os.environ.get("DBSTRING")
|
app.config['DBCONNSTRING'] = os.environ.get("DBSTRING")
|
||||||
else:
|
else:
|
||||||
app.config['DBCONNSTRING'] = ""
|
app.config['DBCONNSTRING'] = ""
|
||||||
elif os.path.isfile(".dbconn"):
|
elif os.path.isfile(".dbconn"):
|
||||||
with open('.dbconn', 'r') as file:
|
with open('.dbconn', 'r') as file:
|
||||||
data = file.read().replace('\n', '')
|
data = file.read().replace('\n', '')
|
||||||
@ -74,15 +82,31 @@ def load_dbconfig(app: Flask):
|
|||||||
exit("No database connection string found. Cannot continue. Please set the environment variable DBSTRING or create a file .dbconn in the root directory of the project.")
|
exit("No database connection string found. Cannot continue. Please set the environment variable DBSTRING or create a file .dbconn in the root directory of the project.")
|
||||||
|
|
||||||
# Check if config exists in DB, if not, create it.
|
# Check if config exists in DB, if not, create it.
|
||||||
|
|
||||||
|
|
||||||
def setup_config(app: Flask):
|
def setup_config(app: Flask):
|
||||||
if check_config_exists():
|
if check_config_exists() == False:
|
||||||
config = database.get_config_list()
|
print("No config found, creating new config"):
|
||||||
print("Loaded existing config")
|
initial_username = os.environ.get("INITIAL_USERNAME")
|
||||||
else:
|
initial_password = os.environ.get("INITIAL_PASSWORD")
|
||||||
config = {'username': 'admin', 'password': 'changeme', 'entryquota': 3, 'maxqueue': 20, 'entries_allowed': 1, 'theme': 'default.css'}
|
if initial_username is None:
|
||||||
for key, value in config.items():
|
print(
|
||||||
|
"No initial username set. Please set the environment variable INITIAL_USERNAME")
|
||||||
|
exit()
|
||||||
|
if initial_password is None:
|
||||||
|
print(
|
||||||
|
"No initial password set. Please set the environment variable INITIAL_PASSWORD")
|
||||||
|
exit()
|
||||||
|
default_config = {'username': initial_username,
|
||||||
|
'password': initial_password,
|
||||||
|
'entryquota': 3,
|
||||||
|
'maxqueue': 20,
|
||||||
|
'entries_allowed': 1,
|
||||||
|
'theme': 'default.css'}
|
||||||
|
for key, value in default_config.items():
|
||||||
database.set_config(key, value)
|
database.set_config(key, value)
|
||||||
print("Created new config")
|
print("Created new config")
|
||||||
|
config = database.get_config_list()
|
||||||
app.config['BASIC_AUTH_USERNAME'] = config['username']
|
app.config['BASIC_AUTH_USERNAME'] = config['username']
|
||||||
app.config['BASIC_AUTH_PASSWORD'] = config['password']
|
app.config['BASIC_AUTH_PASSWORD'] = config['password']
|
||||||
app.config['ENTRY_QUOTA'] = config['entryquota']
|
app.config['ENTRY_QUOTA'] = config['entryquota']
|
||||||
@ -91,6 +115,8 @@ def setup_config(app: Flask):
|
|||||||
app.config['THEME'] = config['theme']
|
app.config['THEME'] = config['theme']
|
||||||
|
|
||||||
# set queue admittance
|
# set queue admittance
|
||||||
|
|
||||||
|
|
||||||
def set_accept_entries(app: Flask, allowed: bool):
|
def set_accept_entries(app: Flask, allowed: bool):
|
||||||
if allowed:
|
if allowed:
|
||||||
app.config['ENTRIES_ALLOWED'] = True
|
app.config['ENTRIES_ALLOWED'] = True
|
||||||
@ -100,6 +126,8 @@ def set_accept_entries(app: Flask, allowed: bool):
|
|||||||
database.set_config('entries_allowed', '0')
|
database.set_config('entries_allowed', '0')
|
||||||
|
|
||||||
# get queue admittance
|
# get queue admittance
|
||||||
|
|
||||||
|
|
||||||
def get_accept_entries(app: Flask) -> bool:
|
def get_accept_entries(app: Flask) -> bool:
|
||||||
state = bool(int(database.get_config('entries_allowed')))
|
state = bool(int(database.get_config('entries_allowed')))
|
||||||
app.config['ENTRIES_ALLOWED'] = state
|
app.config['ENTRIES_ALLOWED'] = state
|
||||||
@ -108,11 +136,14 @@ def get_accept_entries(app: Flask) -> bool:
|
|||||||
|
|
||||||
# Write settings from current app.config to DB
|
# Write settings from current app.config to DB
|
||||||
def persist_config(app: Flask):
|
def persist_config(app: Flask):
|
||||||
config = {'username': app.config['BASIC_AUTH_USERNAME'], 'password': app.config['BASIC_AUTH_PASSWORD'], 'entryquota': app.config['ENTRY_QUOTA'], 'maxqueue': app.config['MAX_QUEUE']}
|
config = {'username': app.config['BASIC_AUTH_USERNAME'], 'password': app.config['BASIC_AUTH_PASSWORD'],
|
||||||
|
'entryquota': app.config['ENTRY_QUOTA'], 'maxqueue': app.config['MAX_QUEUE']}
|
||||||
for key, value in config.items():
|
for key, value in config.items():
|
||||||
database.set_config(key, value)
|
database.set_config(key, value)
|
||||||
|
|
||||||
# Get available themes from themes directory
|
# Get available themes from themes directory
|
||||||
|
|
||||||
|
|
||||||
def get_themes():
|
def get_themes():
|
||||||
themes = []
|
themes = []
|
||||||
for theme in os.listdir('./static/css/themes'):
|
for theme in os.listdir('./static/css/themes'):
|
||||||
@ -120,6 +151,8 @@ def get_themes():
|
|||||||
return themes
|
return themes
|
||||||
|
|
||||||
# Set theme
|
# Set theme
|
||||||
|
|
||||||
|
|
||||||
def set_theme(app: Flask, theme: str):
|
def set_theme(app: Flask, theme: str):
|
||||||
if theme in get_themes():
|
if theme in get_themes():
|
||||||
app.config['THEME'] = theme
|
app.config['THEME'] = theme
|
||||||
@ -137,5 +170,5 @@ def nocache(view):
|
|||||||
response.headers['Pragma'] = 'no-cache'
|
response.headers['Pragma'] = 'no-cache'
|
||||||
response.headers['Expires'] = '-1'
|
response.headers['Expires'] = '-1'
|
||||||
return response
|
return response
|
||||||
|
|
||||||
return update_wrapper(no_cache, view)
|
return update_wrapper(no_cache, view)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user