This commit is contained in:
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user