diff --git a/.vscode/launch.json b/.vscode/launch.json index 2e00725..27002f1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "preLaunchTask": "versiondump", + "preLaunchTask": "mariadb", "name": "Python: Flask", "type": "python", "cwd": "${workspaceFolder}/backend", @@ -14,7 +14,8 @@ "env": { "FLASK_APP": "backend/app.py", "FLASK_ENV": "development", - "FLASK_DEBUG": "1" + "FLASK_DEBUG": "1", + "DBSTRING": "mysql://devuser:devpw@127.0.0.1:3306/karaoqueue" }, "args": [ "run", @@ -24,7 +25,7 @@ "jinja": true }, { - "preLaunchTask": "versiondump", + "preLaunchTask": "mariadb", "name": "Python: Flask (with reload)", "type": "python", "cwd": "${workspaceFolder}/backend", @@ -42,7 +43,7 @@ "jinja": true }, { - "preLaunchTask": "versiondump", + "preLaunchTask": "mariadb", "name": "Python: Flask (with reload, externally reachable)", "type": "python", "cwd": "${workspaceFolder}/backend", @@ -61,7 +62,7 @@ "jinja": true }, { - "preLaunchTask": "versiondump", + "preLaunchTask": "mariadb", "name": "Python: Flask (externally reachable)", "type": "python", "cwd": "${workspaceFolder}/backend", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 563a855..bdaee63 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,6 +8,13 @@ "type": "shell", "command": "echo \"$(git rev-parse --abbrev-ref HEAD)-$(git describe)\"> ${workspaceFolder}/backend/.version", "problemMatcher": [] + }, + { + "label": "mariadb", + "type": "shell", + "command": "docker run --rm --name some-mariadb --env MARIADB_USER=devuser --env MARIADB_PASSWORD=devpw --env MARIADB_ROOT_PASSWORD=devrootpw --env MARIADB_DATABASE=karaoqueue -p 3306:3306 mariadb:latest", + "isBackground": true, + "activeOnStart": false } ] } \ No newline at end of file diff --git a/backend/Dockerfile b/Dockerfile similarity index 88% rename from backend/Dockerfile rename to Dockerfile index 0e36a09..7389e11 100644 --- a/backend/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ RUN pip install Flask-BasicAuth RUN pip install bs4 -COPY ./app /app \ No newline at end of file +COPY ./backend /app \ No newline at end of file diff --git a/backend/app.py b/backend/app.py index a36000c..370c2b6 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,4 +1,5 @@ -from flask import Flask, render_template, Response, abort, request, redirect, send_from_directory +from flask import Flask, render_template, abort, request, redirect, send_from_directory, jsonify +from flask.wrappers import Request, Response import helpers import database import data_adapters @@ -9,7 +10,7 @@ from helpers import nocache app = Flask(__name__, static_url_path='/static') basic_auth = BasicAuth(app) -accept_entries = False +accept_entries = True @app.route("/") def home(): @@ -81,12 +82,12 @@ def settings(): def settings_post(): entryquota = request.form.get("entryquota") maxqueue = request.form.get("maxqueue") - if entryquota.isnumeric() and int(entryquota) > 0: - app.config['ENTRY_QUOTA'] = int(entryquota) + if entryquota.isnumeric() and int(entryquota) > 0: # type: ignore + app.config['ENTRY_QUOTA'] = int(entryquota) # type: ignore else: abort(400) - if maxqueue.isnumeric and int(maxqueue) > 0: - app.config['MAX_QUEUE'] = int(maxqueue) + if maxqueue.isnumeric and int(maxqueue) > 0: # type: ignore + app.config['MAX_QUEUE'] = int(maxqueue) # type: ignore else: abort(400) @@ -131,8 +132,8 @@ def get_song_completions(input_string=""): input_string = request.args.get('search', input_string) if input_string != "": print(input_string) - list = database.get_song_completions(input_string=input_string) - return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') + result = [list(x) for x in database.get_song_completions(input_string=input_string)] + return jsonify(result) else: return 400 @@ -229,6 +230,7 @@ def admin(): @app.before_first_request def activate_job(): + helpers.load_dbconfig(app) helpers.load_version(app) helpers.create_data_directory() database.create_entry_table() diff --git a/backend/database.py b/backend/database.py index 8fe2cd2..fe9805d 100644 --- a/backend/database.py +++ b/backend/database.py @@ -1,31 +1,39 @@ # -*- coding: utf_8 -*- -import sqlite3 +from email.mime import base +from MySQLdb import Connection +from sqlalchemy import create_engine, engine import pandas from io import StringIO +from flask import current_app song_table = "songs" entry_table = "entries" index_label = "Id" done_table = "done_songs" +connection = None -def open_db(): - conn = sqlite3.connect("/tmp/karaoqueue.db") - conn.execute('PRAGMA encoding = "UTF-8";') - return conn + +def open_db() -> engine.base.Connection: + global connection + if (not connection): + print(current_app.config.get("DBCONNSTRING")) + engine = create_engine(current_app.config.get("DBCONNSTRING")) # type: ignore + connection = engine.connect() + # cur.execute('PRAGMA encoding = "UTF-8";') + return connection def import_songs(song_csv): print("Start importing Songs...") df = pandas.read_csv(StringIO(song_csv), sep=';') conn = open_db() - cur = conn.cursor() df.to_sql(song_table, conn, if_exists='replace', index=False) - cur.execute("SELECT Count(Id) FROM songs") - num_songs = cur.fetchone()[0] - conn.close() + cur = conn.execute("SELECT Count(Id) FROM songs") + num_songs = cur.fetchone()[0] # type: ignore + # conn.close() print("Imported songs ({} in Database)".format(num_songs)) return("Imported songs ({} in Database)".format(num_songs)) @@ -33,158 +41,146 @@ def import_songs(song_csv): def create_entry_table(): conn = open_db() conn.execute('CREATE TABLE IF NOT EXISTS '+entry_table + - ' (ID INTEGER PRIMARY KEY NOT NULL, Song_Id INTEGER NOT NULL, Name VARCHAR(255), Client_Id VARCHAR(36), Transferred INTEGER DEFAULT 0)') - conn.close() + ' (ID INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT, Song_Id INTEGER NOT NULL, Name VARCHAR(255), Client_Id VARCHAR(36), Transferred INTEGER DEFAULT 0)') + # conn.close() def create_done_song_table(): conn = open_db() conn.execute('CREATE TABLE IF NOT EXISTS '+done_table + ' (Song_Id INTEGER PRIMARY KEY NOT NULL, Plays INTEGER)') - conn.close() + # conn.close() def create_song_table(): conn = open_db() - conn.execute("CREATE TABLE IF NOT EXISTS \""+song_table+"""\" ( - "Id" INTEGER, - "Title" TEXT, - "Artist" TEXT, - "Year" INTEGER, - "Duo" INTEGER, - "Explicit" INTEGER, - "Date Added" TEXT, - "Styles" TEXT, - "Languages" TEXT + conn.execute("CREATE TABLE IF NOT EXISTS `"+song_table+"""` ( + `Id` INTEGER, + `Title` TEXT, + `Artist` TEXT, + `Year` INTEGER, + `Duo` INTEGER, + `Explicit` INTEGER, + `Date Added` TEXT, + `Styles` TEXT, + `Languages` TEXT )""") - conn.close() + # conn.close() def create_list_view(): conn = open_db() - conn.execute("""CREATE VIEW IF NOT EXISTS [Liste] AS - SELECT Name, Title, Artist, entries.Id, songs.Id, entries.Transferred + conn.execute("""CREATE OR REPLACE VIEW `Liste` AS + SELECT Name, Title, Artist, entries.Id AS entry_ID, songs.Id AS song_ID, entries.Transferred FROM entries, songs WHERE entries.Song_Id=songs.Id""") - conn.close() + # conn.close() def create_done_song_view(): conn = open_db() - conn.execute("""CREATE VIEW IF NOT EXISTS [Abspielliste] AS - SELECT Artist || \" - \" || Title AS Song, Plays AS Wiedergaben + conn.execute("""CREATE OR REPLACE VIEW `Abspielliste` AS + SELECT CONCAT(Artist," - ", Title) AS Song, Plays AS Wiedergaben FROM songs, done_songs WHERE done_songs.Song_Id=songs.Id""") - conn.close() + # conn.close() def get_list(): conn = open_db() - conn.row_factory = sqlite3.Row - cur = conn.cursor() - cur.execute("SELECT * FROM Liste") + cur = conn.execute("SELECT * FROM Liste") return cur.fetchall() def get_played_list(): conn = open_db() - cur = conn.cursor() - cur.execute("SELECT * FROM Abspielliste") + cur = conn.execute("SELECT * FROM Abspielliste") return cur.fetchall() def get_song_list(): conn = open_db() - cur = conn.cursor() - cur.execute("SELECT Artist || \" - \" || Title AS Song, Id FROM songs;") + cur = conn.execute( + "SELECT Artist || \" - \" || Title AS Song, Id FROM songs;") return cur.fetchall() def get_song_completions(input_string): conn = open_db() - cur = conn.cursor() # Don't look, it burns... prepared_string = "%{0}%".format( input_string).upper() # "Test" -> "%TEST%" print(prepared_string) - cur.execute( - "SELECT Title || \" - \" || Artist AS Song, Id FROM songs WHERE REPLACE(REPLACE(REPLACE(REPLACE(UPPER( SONG ),'ö','Ö'),'ü','Ü'),'ä','Ä'),'ß','ẞ') LIKE (?) LIMIT 20;", (prepared_string,)) + cur = conn.execute( + "SELECT CONCAT(Artist,\" - \",Title) AS Song, Id FROM songs WHERE CONCAT(Artist,\" - \",Title) LIKE (%s) LIMIT 20;", [prepared_string]) return cur.fetchall() def add_entry(name, song_id, client_id): conn = open_db() - cur = conn.cursor() - cur.execute( - "INSERT INTO entries (Song_Id,Name,Client_Id) VALUES(?,?,?);", (song_id, name, client_id)) - conn.commit() - conn.close() + conn.execute( + "INSERT INTO entries (Song_Id,Name,Client_Id) VALUES(%s,%s,%s);", (song_id, name, client_id)) + # conn.close() return def add_sung_song(entry_id): conn = open_db() - cur = conn.cursor() - cur.execute("""SELECT Song_Id FROM entries WHERE Id=?""", (entry_id,)) - song_id = cur.fetchone()[0] - cur.execute("""INSERT OR REPLACE INTO done_songs (Song_Id, Plays) - VALUES("""+str(song_id)+""", - COALESCE( - (SELECT Plays FROM done_songs - WHERE Song_Id="""+str(song_id)+"), 0) + 1)" - ) - conn.commit() + cur = conn.execute( + """SELECT Song_Id FROM entries WHERE Id=%s""", (entry_id,)) + song_id = cur.fetchone()[0] # type: ignore + conn.execute("""INSERT INTO done_songs (Song_Id, Plays) VALUES("""+str(song_id)+""",1) ON DUPLICATE KEY UPDATE Plays=Plays + 1;""") +# SQLite bullshittery +# conn.execute("""REPLACE INTO done_songs (Song_Id, Plays) +# VALUES("""+str(song_id)+""", +# COALESCE( +# (SELECT Plays FROM done_songs +# WHERE Song_Id="""+str(song_id)+"), 0) + 1)" +# ) delete_entry(entry_id) - conn.close() + # conn.close() return True def toggle_transferred(entry_id): conn = open_db() - cur = conn.cursor() - cur.execute("SELECT Transferred FROM entries WHERE ID =?", (entry_id,)) + cur = conn.execute( + "SELECT Transferred FROM entries WHERE ID =%s", (entry_id,)) marked = cur.fetchall()[0][0] if(marked == 0): - cur.execute( - "UPDATE entries SET Transferred = 1 WHERE ID =?", (entry_id,)) + conn.execute( + "UPDATE entries SET Transferred = 1 WHERE ID =%s", (entry_id,)) else: - cur.execute( - "UPDATE entries SET Transferred = 0 WHERE ID =?", (entry_id,)) - conn.commit() - conn.close() + conn.execute( + "UPDATE entries SET Transferred = 0 WHERE ID =%s", (entry_id,)) + # conn.close() return True def check_entry_quota(client_id): conn = open_db() - cur = conn.cursor() - cur.execute( - "SELECT Count(*) FROM entries WHERE entries.Client_Id = ?", (client_id,)) + cur = conn.execute( + "SELECT Count(*) FROM entries WHERE entries.Client_Id = %s", (client_id,)) return cur.fetchall()[0][0] def check_queue_length(): conn = open_db() - cur = conn.cursor() - cur.execute("SELECT Count(*) FROM entries") + cur = conn.execute("SELECT Count(*) FROM entries") return cur.fetchall()[0][0] def clear_played_songs(): conn = open_db() - cur = conn.cursor() - cur.execute("DELETE FROM done_songs") - conn.commit() - conn.close() + conn.execute("DELETE FROM done_songs") + # conn.close() return True def delete_entry(id): conn = open_db() - cur = conn.cursor() - cur.execute("DELETE FROM entries WHERE id=?", (id,)) - conn.commit() - conn.close() + conn.execute("DELETE FROM entries WHERE id=%s", (id,)) + # conn.close() return True @@ -194,19 +190,15 @@ def delete_entries(ids): idlist.append((x,)) try: conn = open_db() - cur = conn.cursor() - cur.executemany("DELETE FROM entries WHERE id=?", idlist) - conn.commit() - conn.close() + cur = conn.execute("DELETE FROM entries WHERE id=%s", idlist) + # conn.close() return cur.rowcount - except sqlite3.Error as error: + except Exception as error: return -1 def delete_all_entries(): conn = open_db() - cur = conn.cursor() - cur.execute("DELETE FROM entries") - conn.commit() - conn.close() + conn.execute("DELETE FROM entries") + # conn.close() return True diff --git a/backend/helpers.py b/backend/helpers.py index 17545a6..656a6bf 100644 --- a/backend/helpers.py +++ b/backend/helpers.py @@ -37,7 +37,7 @@ def check_config_exists(): def load_version(app): if os.environ.get("SOURCE_VERSION"): - app.config['VERSION'] = os.environ.get("SOURCE_VERSION")[0:7] + app.config['VERSION'] = os.environ.get("SOURCE_VERSION")[0:7] # type: ignore elif os.path.isfile(".version"): with open('.version', 'r') as file: data = file.read().replace('\n', '') @@ -47,22 +47,52 @@ def load_version(app): app.config['VERSION'] = "" else: app.config['VERSION'] = "" + +def load_dbconfig(app): + if os.environ.get("FLASK_ENV") == "development": + app.config['DBCONNSTRING'] = os.environ.get("DBSTRING") + else: + if os.environ.get("DEPLOYMENT_PLATFORM") == "Heroku": + if os.environ.get("JAWSDB_MARIA_URL"): + app.config['DBCONNSTRING'] = os.environ.get("JAWSDB_MARIA_URL") + else: + app.config['DBCONNSTRING'] = "" + if os.environ.get("DEPLOYMENT_PLATFORM") == "Docker": + if os.environ.get("DBSTRING"): + app.config['DBCONNSTRING'] = os.environ.get("DBSTRING") + else: + app.config['DBCONNSTRING'] = "" + elif os.path.isfile(".dbconn"): + with open('.dbconn', 'r') as file: + data = file.read().replace('\n', '') + if data: + app.config['DBCONNSTRING'] = data + else: + app.config['DBCONNSTRING'] = "" + else: + app.config['DBCONNSTRING'] = "" def setup_config(app): - if check_config_exists(): - config = json.load(open(config_file)) - with open(config_file, 'r') as handle: - config = json.load(handle) - print("Loaded existing config") + if os.environ.get("DEPLOYMENT_PLATFORM") == "Docker": + app.config['BASIC_AUTH_USERNAME'] = os.environ.get('BASIC_AUTH_USERNAME') + app.config['BASIC_AUTH_PASSWORD'] = os.environ.get('BASIC_AUTH_PASSWORD') + app.config['ENTRY_QUOTA'] = os.environ.get('ENTRY_QUOTA') + app.config['MAX_QUEUE'] = os.environ.get('MAX_QUEUE') else: - config = {'username': 'admin', 'password': 'changeme', 'entryquota': 3, 'maxqueue': 20} - with open(config_file, 'w') as handle: - json.dump(config, handle, indent=4, sort_keys=True) - print("Wrote new config") - app.config['BASIC_AUTH_USERNAME'] = config['username'] - app.config['BASIC_AUTH_PASSWORD'] = config['password'] - app.config['ENTRY_QUOTA'] = config['entryquota'] - app.config['MAX_QUEUE'] = config['maxqueue'] + if check_config_exists(): + config = json.load(open(config_file)) + with open(config_file, 'r') as handle: + config = json.load(handle) + print("Loaded existing config") + else: + config = {'username': 'admin', 'password': 'changeme', 'entryquota': 3, 'maxqueue': 20} + with open(config_file, 'w') as handle: + json.dump(config, handle, indent=4, sort_keys=True) + print("Wrote new config") + app.config['BASIC_AUTH_USERNAME'] = config['username'] + app.config['BASIC_AUTH_PASSWORD'] = config['password'] + app.config['ENTRY_QUOTA'] = config['entryquota'] + app.config['MAX_QUEUE'] = config['maxqueue'] @@ -70,7 +100,7 @@ def nocache(view): @wraps(view) def no_cache(*args, **kwargs): response = make_response(view(*args, **kwargs)) - response.headers['Last-Modified'] = datetime.now() + response.headers['Last-Modified'] = datetime.now() # type: ignore response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0' response.headers['Pragma'] = 'no-cache' response.headers['Expires'] = '-1' diff --git a/backend/requirements.txt b/backend/requirements.txt index 04324d8..d816517 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -2,4 +2,6 @@ requests pandas Flask-BasicAuth bs4 -gunicorn \ No newline at end of file +gunicorn +SQLAlchemy +mysqlclient \ No newline at end of file diff --git a/backend/templates/base.html b/backend/templates/base.html index f243850..8b598ea 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -75,7 +75,7 @@ {% endif %} - KaraoQueue {{karaoqueue_version}} - © 2019-21 - Phillip + KaraoQueue {{karaoqueue_version}} - 2019-22 - Phillip Kühne diff --git a/backend/templates/main_admin.html b/backend/templates/main_admin.html index 5234a88..5379dc2 100644 --- a/backend/templates/main_admin.html +++ b/backend/templates/main_admin.html @@ -52,10 +52,10 @@ table td:nth-child(2) { {% block extrajs %} diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..2ee48c0 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,23 @@ +version: "3.9" +services: + karaoqueue: + image: "phillipkhne/karaoqueue:latest" + restart: always + ports: + - "127.0.0.1:8081:80" + environment: + DEPLOYMENT_PLATFORM: Docker + DBSTRING: mysql://user:pass@host:3306/database + BASIC_AUTH_USERNAME: admin + BASIC_AUTH_PASSWORD: changeme + ENTRY_QUOTA: 3 + MAX_QUEUE: 20 + db: + image: mariadb + restart: always + environment: + MARIADB_ROOT_PASSWORD: dpMAZj*Mc4%FZM!V + MARIADB_ROOT_HOST: localhost + MARIADB_DATABASE: karaoqueue + MARIADB_USER: karaoqueue + MARIADB_PASSWORD: a5G@P*^tCW$$w@wE \ No newline at end of file