diff --git a/Dockers/puppeteer-api/Dockerfile b/Dockers/puppeteer-api/Dockerfile index cdc9047..bbd39ff 100644 --- a/Dockers/puppeteer-api/Dockerfile +++ b/Dockers/puppeteer-api/Dockerfile @@ -40,7 +40,7 @@ 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 && chmod 777 /db +RUN mkdir -p /db && chmod 777 /db && chown -R puppeteer:puppeteer /app USER puppeteer diff --git a/Dockers/puppeteer-api/main.py b/Dockers/puppeteer-api/main.py index d61bf07..cfaae7d 100644 --- a/Dockers/puppeteer-api/main.py +++ b/Dockers/puppeteer-api/main.py @@ -23,16 +23,37 @@ CUSTOM_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.3 # Initialize SQLite database def init_db(): global DB_PATH + + # Try to use the mounted volume first db_path = '/db/cache.db' db_dir = os.path.dirname(db_path) - # Ensure the directory exists - if not os.path.exists(db_dir): + # Check if directory exists and is writable + dir_writable = False + if os.path.exists(db_dir): + try: + test_file = os.path.join(db_dir, '.write_test') + with open(test_file, 'w') as f: + f.write('test') + os.remove(test_file) + dir_writable = True + except (IOError, PermissionError): + print(f"Directory {db_dir} exists but is not writable") + dir_writable = False + + # If directory doesn't exist or isn't writable, try to create it + if not os.path.exists(db_dir) or not dir_writable: try: os.makedirs(db_dir, exist_ok=True) + # Test if we can write to the directory + test_file = os.path.join(db_dir, '.write_test') + with open(test_file, 'w') as f: + f.write('test') + os.remove(test_file) print(f"Created directory: {db_dir}") + dir_writable = True except Exception as e: - print(f"Warning: Could not create directory {db_dir}: {e}") + print(f"Warning: Could not create or write to directory {db_dir}: {e}") # Fallback to using a local database file db_path = 'cache.db' print(f"Using local database file: {db_path}") @@ -55,26 +76,30 @@ def init_db(): # Update the global DB_PATH DB_PATH = db_path except sqlite3.OperationalError as e: - print(f"Error initializing database: {e}") + print(f"Error initializing database at {db_path}: {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 - DB_PATH = 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"Local database initialized at {db_path}") + # Update the global DB_PATH + DB_PATH = db_path + except sqlite3.OperationalError as e2: + print(f"Error initializing local database: {e2}") + raise # Define the database path DB_PATH = '/db/cache.db'