mirror of
https://github.com/krestaino/sptnr.git
synced 2026-06-12 22:15:22 -04:00
refactor: web api key
This commit is contained in:
+3
-1
@@ -2,4 +2,6 @@ NAV_BASE_URL=your_navidrome_server_url
|
|||||||
NAV_USER=your_navidrome_username
|
NAV_USER=your_navidrome_username
|
||||||
NAV_PASS=your_navidrome_password
|
NAV_PASS=your_navidrome_password
|
||||||
SPOTIFY_CLIENT_ID=your_spotify_client_id
|
SPOTIFY_CLIENT_ID=your_spotify_client_id
|
||||||
SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
|
SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
|
||||||
|
WEB_API_KEY=changeme
|
||||||
|
ENABLE_WEB_API_KEY=True
|
||||||
+3
-6
@@ -10,11 +10,8 @@ COPY . .
|
|||||||
# Install any needed packages specified in requirements.txt
|
# Install any needed packages specified in requirements.txt
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
# Expose port 5000 for the Flask app
|
# Expose port 3333 for the Flask app
|
||||||
EXPOSE 5000
|
EXPOSE 3333
|
||||||
|
|
||||||
# Set the entrypoint to Python
|
# Set the entrypoint to Python
|
||||||
ENTRYPOINT ["python"]
|
ENTRYPOINT ["python", "sptnr.py"]
|
||||||
|
|
||||||
# Leave CMD empty
|
|
||||||
CMD []
|
|
||||||
|
|||||||
@@ -16,8 +16,13 @@ services:
|
|||||||
- SPOTIFY_CLIENT_ID=your_spotify_client_id
|
- SPOTIFY_CLIENT_ID=your_spotify_client_id
|
||||||
- SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
|
- SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
|
||||||
- WEB_API_KEY=changeme
|
- WEB_API_KEY=changeme
|
||||||
|
- ENABLE_WEB_API_KEY=False
|
||||||
volumes:
|
volumes:
|
||||||
- ./data:/usr/src/app/data
|
- ./data:/usr/src/app/data
|
||||||
ports:
|
ports:
|
||||||
- "3333:3333"
|
- "3333:3333"
|
||||||
command: web_server.py
|
# Uncomment the next line to just run the script
|
||||||
|
# entrypoint: ["python", "sptnr.py"]
|
||||||
|
|
||||||
|
# Uncomment the next line to start the web server
|
||||||
|
# entrypoint: ["python", "web_server.py"]
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ LOG_DIR = "data/logs"
|
|||||||
if not os.path.exists(LOG_DIR):
|
if not os.path.exists(LOG_DIR):
|
||||||
os.makedirs(LOG_DIR)
|
os.makedirs(LOG_DIR)
|
||||||
|
|
||||||
LOGFILE = os.path.join(LOG_DIR, f"spotify-popularity_{int(time.time())}.log")
|
LOGFILE = os.path.join(LOG_DIR, f"sptnr_{int(time.time())}.log")
|
||||||
|
|
||||||
|
|
||||||
class NoColorFormatter(logging.Formatter):
|
class NoColorFormatter(logging.Formatter):
|
||||||
|
|||||||
+16
-13
@@ -1,34 +1,35 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
import json
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import threading
|
import threading
|
||||||
import os
|
import os
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import datetime
|
import functools
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
sptnr_web_server = Flask(__name__)
|
sptnr_web_server = Flask(__name__)
|
||||||
API_KEY = os.getenv("WEB_API_KEY")
|
WEB_API_KEY = os.getenv("WEB_API_KEY")
|
||||||
|
ENABLE_WEB_API_KEY = os.getenv("ENABLE_WEB_API_KEY", "True") == "True"
|
||||||
LOG_DIR = "data/logs"
|
LOG_DIR = "data/logs"
|
||||||
|
|
||||||
|
|
||||||
|
def api_key_required(f):
|
||||||
|
@functools.wraps(f)
|
||||||
|
def decorated_function(*args, **kwargs):
|
||||||
|
if ENABLE_WEB_API_KEY and request.args.get("api_key") != WEB_API_KEY:
|
||||||
|
return jsonify({"error": "Unauthorized"}), 401
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
|
return decorated_function
|
||||||
|
|
||||||
|
|
||||||
def run_script(cmd):
|
def run_script(cmd):
|
||||||
subprocess.run(cmd)
|
subprocess.run(cmd)
|
||||||
|
|
||||||
|
|
||||||
def log_post_data(data):
|
|
||||||
timestamp = datetime.datetime.now().isoformat()
|
|
||||||
log_filename = os.path.join(LOG_DIR, f"log_{timestamp}.txt")
|
|
||||||
with open(log_filename, "w") as log_file:
|
|
||||||
json.dump(data, log_file, indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
@sptnr_web_server.route("/process", methods=["GET", "POST"])
|
@sptnr_web_server.route("/process", methods=["GET", "POST"])
|
||||||
|
@api_key_required
|
||||||
def process_request():
|
def process_request():
|
||||||
if request.args.get("api_key") != API_KEY:
|
|
||||||
return jsonify({"error": "Unauthorized"}), 401
|
|
||||||
|
|
||||||
cmd = ["python3", "sptnr.py"]
|
cmd = ["python3", "sptnr.py"]
|
||||||
if request.args.get("preview", "") == "true":
|
if request.args.get("preview", "") == "true":
|
||||||
cmd.append("--preview")
|
cmd.append("--preview")
|
||||||
@@ -54,6 +55,7 @@ def process_request():
|
|||||||
|
|
||||||
|
|
||||||
@sptnr_web_server.route("/logs")
|
@sptnr_web_server.route("/logs")
|
||||||
|
@api_key_required
|
||||||
def list_logs():
|
def list_logs():
|
||||||
try:
|
try:
|
||||||
logs = os.listdir(LOG_DIR)
|
logs = os.listdir(LOG_DIR)
|
||||||
@@ -66,6 +68,7 @@ def list_logs():
|
|||||||
|
|
||||||
|
|
||||||
@sptnr_web_server.route("/logs/<filename>")
|
@sptnr_web_server.route("/logs/<filename>")
|
||||||
|
@api_key_required
|
||||||
def view_log(filename):
|
def view_log(filename):
|
||||||
try:
|
try:
|
||||||
full_path = os.path.join(LOG_DIR, filename)
|
full_path = os.path.join(LOG_DIR, filename)
|
||||||
|
|||||||
Reference in New Issue
Block a user