This commit is contained in:
@@ -40,7 +40,7 @@ ENV API_KEY=""
|
|||||||
# Create a non-root user and switch to it
|
# Create a non-root user and switch to it
|
||||||
RUN useradd -m puppeteer && chown -R puppeteer:puppeteer /app
|
RUN useradd -m puppeteer && chown -R puppeteer:puppeteer /app
|
||||||
# Create the database directory and set permissions
|
# 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
|
USER puppeteer
|
||||||
|
|
||||||
|
|||||||
@@ -23,16 +23,37 @@ CUSTOM_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.3
|
|||||||
# Initialize SQLite database
|
# Initialize SQLite database
|
||||||
def init_db():
|
def init_db():
|
||||||
global DB_PATH
|
global DB_PATH
|
||||||
|
|
||||||
|
# Try to use the mounted volume first
|
||||||
db_path = '/db/cache.db'
|
db_path = '/db/cache.db'
|
||||||
db_dir = os.path.dirname(db_path)
|
db_dir = os.path.dirname(db_path)
|
||||||
|
|
||||||
# Ensure the directory exists
|
# Check if directory exists and is writable
|
||||||
if not os.path.exists(db_dir):
|
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:
|
try:
|
||||||
os.makedirs(db_dir, exist_ok=True)
|
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}")
|
print(f"Created directory: {db_dir}")
|
||||||
|
dir_writable = True
|
||||||
except Exception as e:
|
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
|
# Fallback to using a local database file
|
||||||
db_path = 'cache.db'
|
db_path = 'cache.db'
|
||||||
print(f"Using local database file: {db_path}")
|
print(f"Using local database file: {db_path}")
|
||||||
@@ -55,26 +76,30 @@ def init_db():
|
|||||||
# Update the global DB_PATH
|
# Update the global DB_PATH
|
||||||
DB_PATH = db_path
|
DB_PATH = db_path
|
||||||
except sqlite3.OperationalError as e:
|
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
|
# Fallback to using a local database file if the mounted volume has permission issues
|
||||||
db_path = 'cache.db'
|
db_path = 'cache.db'
|
||||||
print(f"Falling back to local database file: {db_path}")
|
print(f"Falling back to local database file: {db_path}")
|
||||||
conn = sqlite3.connect(db_path)
|
try:
|
||||||
cursor = conn.cursor()
|
conn = sqlite3.connect(db_path)
|
||||||
cursor.execute('''
|
cursor = conn.cursor()
|
||||||
CREATE TABLE IF NOT EXISTS cache (
|
cursor.execute('''
|
||||||
url TEXT,
|
CREATE TABLE IF NOT EXISTS cache (
|
||||||
route TEXT,
|
url TEXT,
|
||||||
data TEXT,
|
route TEXT,
|
||||||
timestamp INTEGER,
|
data TEXT,
|
||||||
PRIMARY KEY (url, route)
|
timestamp INTEGER,
|
||||||
)
|
PRIMARY KEY (url, route)
|
||||||
''')
|
)
|
||||||
conn.commit()
|
''')
|
||||||
conn.close()
|
conn.commit()
|
||||||
print(f"Local database initialized at {db_path}")
|
conn.close()
|
||||||
# Update the global DB_PATH
|
print(f"Local database initialized at {db_path}")
|
||||||
DB_PATH = 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
|
# Define the database path
|
||||||
DB_PATH = '/db/cache.db'
|
DB_PATH = '/db/cache.db'
|
||||||
|
|||||||
Reference in New Issue
Block a user