This commit is contained in:
@@ -39,6 +39,9 @@ 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
|
||||||
|
RUN mkdir -p /db && chown -R puppeteer:puppeteer /db
|
||||||
|
|
||||||
USER puppeteer
|
USER puppeteer
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
|
|||||||
@@ -22,23 +22,65 @@ 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():
|
||||||
conn = sqlite3.connect('/db/cache.db')
|
db_path = '/db/cache.db'
|
||||||
cursor = conn.cursor()
|
db_dir = os.path.dirname(db_path)
|
||||||
cursor.execute('''
|
|
||||||
CREATE TABLE IF NOT EXISTS cache (
|
# Ensure the directory exists
|
||||||
url TEXT,
|
if not os.path.exists(db_dir):
|
||||||
route TEXT,
|
try:
|
||||||
data TEXT,
|
os.makedirs(db_dir, exist_ok=True)
|
||||||
timestamp INTEGER,
|
print(f"Created directory: {db_dir}")
|
||||||
PRIMARY KEY (url, route)
|
except Exception as e:
|
||||||
)
|
print(f"Warning: Could not create directory {db_dir}: {e}")
|
||||||
''')
|
# Fallback to using a local database file
|
||||||
conn.commit()
|
db_path = 'cache.db'
|
||||||
conn.close()
|
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
|
# Get cached data if it exists and is not older than 36 hours
|
||||||
def get_cached_data(url, route):
|
def get_cached_data(url, route):
|
||||||
conn = sqlite3.connect('/db/cache.db')
|
conn = sqlite3.connect(DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cache_expiry = int(time.time()) - (36 * 60 * 60) # 36 hours in seconds
|
cache_expiry = int(time.time()) - (36 * 60 * 60) # 36 hours in seconds
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
@@ -55,7 +97,7 @@ def get_cached_data(url, route):
|
|||||||
|
|
||||||
# Save data to cache
|
# Save data to cache
|
||||||
def save_to_cache(url, route, data):
|
def save_to_cache(url, route, data):
|
||||||
conn = sqlite3.connect('/db/cache.db')
|
conn = sqlite3.connect(DB_PATH)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
timestamp = int(time.time())
|
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:
|
if not x_api_key or x_api_key != API_KEY:
|
||||||
raise HTTPException(status_code=401, detail="Invalid 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 = conn.cursor()
|
||||||
cursor.execute("DELETE FROM cache")
|
cursor.execute("DELETE FROM cache")
|
||||||
conn.commit()
|
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:
|
if not x_api_key or x_api_key != API_KEY:
|
||||||
raise HTTPException(status_code=401, detail="Invalid 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 = conn.cursor()
|
||||||
|
|
||||||
# Get total entries
|
# Get total entries
|
||||||
|
|||||||
Reference in New Issue
Block a user