This commit is contained in:
@@ -22,23 +22,65 @@ 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')
|
||||
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()
|
||||
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 (
|
||||
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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user