fix
Build and Push Docker Images / build-and-push (push) Successful in 20s

This commit is contained in:
2025-04-30 20:26:14 +02:00
parent db7bc85bae
commit dd5aa71220
2 changed files with 62 additions and 17 deletions
+3
View File
@@ -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
+47 -5
View File
@@ -22,7 +22,22 @@ 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')
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 (
@@ -35,10 +50,37 @@ def init_db():
''')
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