From dd5aa71220765c245fa84b3a4b95b89dd5d9c244 Mon Sep 17 00:00:00 2001 From: Bram Kelchtermans Date: Wed, 30 Apr 2025 20:26:14 +0200 Subject: [PATCH] fix --- Dockers/puppeteer-api/Dockerfile | 3 ++ Dockers/puppeteer-api/main.py | 76 +++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/Dockers/puppeteer-api/Dockerfile b/Dockers/puppeteer-api/Dockerfile index 53cf7c8..41491ae 100644 --- a/Dockers/puppeteer-api/Dockerfile +++ b/Dockers/puppeteer-api/Dockerfile @@ -39,6 +39,9 @@ ENV API_KEY="" # Create a non-root user and switch to it RUN useradd -m puppeteer && chown -R puppeteer:puppeteer /app +# Create the database directory and set permissions +RUN mkdir -p /db && chown -R puppeteer:puppeteer /db + USER puppeteer # Expose port diff --git a/Dockers/puppeteer-api/main.py b/Dockers/puppeteer-api/main.py index 498d30c..7f5e20c 100644 --- a/Dockers/puppeteer-api/main.py +++ b/Dockers/puppeteer-api/main.py @@ -22,23 +22,65 @@ CUSTOM_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.3 # Initialize SQLite database def init_db(): - conn = sqlite3.connect('/db/cache.db') - cursor = conn.cursor() - cursor.execute(''' - CREATE TABLE IF NOT EXISTS cache ( - url TEXT, - route TEXT, - data TEXT, - timestamp INTEGER, - PRIMARY KEY (url, route) - ) - ''') - conn.commit() - conn.close() + db_path = '/db/cache.db' + db_dir = os.path.dirname(db_path) + + # Ensure the directory exists + if not os.path.exists(db_dir): + try: + os.makedirs(db_dir, exist_ok=True) + print(f"Created directory: {db_dir}") + except Exception as e: + print(f"Warning: Could not create directory {db_dir}: {e}") + # Fallback to using a local database file + db_path = 'cache.db' + print(f"Using local database file: {db_path}") + + try: + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS cache ( + url TEXT, + route TEXT, + data TEXT, + timestamp INTEGER, + PRIMARY KEY (url, route) + ) + ''') + conn.commit() + conn.close() + print(f"Database initialized at {db_path}") + except sqlite3.OperationalError as e: + print(f"Error initializing database: {e}") + # Fallback to using a local database file if the mounted volume has permission issues + db_path = 'cache.db' + print(f"Falling back to local database file: {db_path}") + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS cache ( + url TEXT, + route TEXT, + data TEXT, + timestamp INTEGER, + PRIMARY KEY (url, route) + ) + ''') + conn.commit() + conn.close() + print(f"Local database initialized at {db_path}") + + # Update the global DB_PATH + global DB_PATH + DB_PATH = db_path + +# Define the database path +DB_PATH = '/db/cache.db' # Get cached data if it exists and is not older than 36 hours def get_cached_data(url, route): - conn = sqlite3.connect('/db/cache.db') + conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cache_expiry = int(time.time()) - (36 * 60 * 60) # 36 hours in seconds cursor.execute( @@ -55,7 +97,7 @@ def get_cached_data(url, route): # Save data to cache def save_to_cache(url, route, data): - conn = sqlite3.connect('/db/cache.db') + conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() timestamp = int(time.time()) @@ -338,7 +380,7 @@ async def clear_cache(x_api_key: Optional[str] = Header(None)): if not x_api_key or x_api_key != API_KEY: raise HTTPException(status_code=401, detail="Invalid API key") - conn = sqlite3.connect('/db/cache.db') + conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute("DELETE FROM cache") conn.commit() @@ -353,7 +395,7 @@ async def cache_stats(x_api_key: Optional[str] = Header(None)): if not x_api_key or x_api_key != API_KEY: raise HTTPException(status_code=401, detail="Invalid API key") - conn = sqlite3.connect('/db/cache.db') + conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() # Get total entries