Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
90d34ea
1
Parent(s): 40ff339
Improve data persistence and error handling
Browse files- Dockerfile +8 -8
- app.py +21 -2
- templates/admin_login.html +3 -2
Dockerfile
CHANGED
|
@@ -14,17 +14,17 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|
| 14 |
# Copy the rest of the application
|
| 15 |
COPY . /code/
|
| 16 |
|
| 17 |
-
# Create
|
| 18 |
-
RUN useradd -m myuser && \
|
| 19 |
-
chown -R myuser:myuser /code
|
| 20 |
-
|
| 21 |
-
# Create data directory for persistent storage
|
| 22 |
RUN mkdir -p /data && \
|
| 23 |
-
chown -R
|
| 24 |
-
chmod 777 /data
|
|
|
|
| 25 |
|
| 26 |
# Switch to non-root user
|
| 27 |
-
USER
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Run the application
|
| 30 |
CMD ["python", "app.py"]
|
|
|
|
| 14 |
# Copy the rest of the application
|
| 15 |
COPY . /code/
|
| 16 |
|
| 17 |
+
# Create data directory and set permissions
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
RUN mkdir -p /data && \
|
| 19 |
+
chown -R 1000:1000 /data && \
|
| 20 |
+
chmod -R 777 /data && \
|
| 21 |
+
chown -R 1000:1000 /code
|
| 22 |
|
| 23 |
# Switch to non-root user
|
| 24 |
+
USER 1000:1000
|
| 25 |
+
|
| 26 |
+
# Set environment variables
|
| 27 |
+
ENV PYTHONUNBUFFERED=1
|
| 28 |
|
| 29 |
# Run the application
|
| 30 |
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from flask import Flask, render_template, request, jsonify, session, redirect, u
|
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from datetime import datetime
|
| 4 |
import os
|
|
|
|
| 5 |
from functools import wraps
|
| 6 |
|
| 7 |
app = Flask(__name__, instance_relative_config=True)
|
|
@@ -15,11 +16,27 @@ ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', '1234') # 預設密碼為 123
|
|
| 15 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 16 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
| 17 |
db_path = '/data/event.db'
|
|
|
|
| 18 |
os.makedirs('/data', exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
else: # 在本地開發環境中
|
| 20 |
db_path = os.path.join(app.instance_path, 'event.db')
|
| 21 |
os.makedirs(app.instance_path, exist_ok=True)
|
| 22 |
|
|
|
|
| 23 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 24 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 25 |
|
|
@@ -27,7 +44,9 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
| 27 |
try:
|
| 28 |
if not os.path.exists(db_path):
|
| 29 |
open(db_path, 'a').close()
|
|
|
|
| 30 |
os.chmod(db_path, 0o666)
|
|
|
|
| 31 |
except OSError as e:
|
| 32 |
print(f"Error setting up database: {e}")
|
| 33 |
|
|
@@ -103,8 +122,8 @@ def admin_login_api():
|
|
| 103 |
data = request.json
|
| 104 |
if data.get('password') == ADMIN_PASSWORD:
|
| 105 |
session['admin_logged_in'] = True
|
| 106 |
-
return jsonify({'message': 'Login successful'})
|
| 107 |
-
return jsonify({'error': 'Invalid password'}), 401
|
| 108 |
|
| 109 |
@app.route('/api/admin/settings', methods=['GET', 'POST'])
|
| 110 |
@admin_required
|
|
|
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from datetime import datetime
|
| 4 |
import os
|
| 5 |
+
import time
|
| 6 |
from functools import wraps
|
| 7 |
|
| 8 |
app = Flask(__name__, instance_relative_config=True)
|
|
|
|
| 16 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 17 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
| 18 |
db_path = '/data/event.db'
|
| 19 |
+
# 確保資料目錄存在
|
| 20 |
os.makedirs('/data', exist_ok=True)
|
| 21 |
+
# 等待資料目錄就緒
|
| 22 |
+
max_retries = 5
|
| 23 |
+
retry_count = 0
|
| 24 |
+
while retry_count < max_retries:
|
| 25 |
+
try:
|
| 26 |
+
if os.access('/data', os.W_OK):
|
| 27 |
+
break
|
| 28 |
+
print(f"Waiting for /data directory to be writable... Attempt {retry_count + 1}/{max_retries}")
|
| 29 |
+
time.sleep(1)
|
| 30 |
+
retry_count += 1
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error checking /data directory: {e}")
|
| 33 |
+
time.sleep(1)
|
| 34 |
+
retry_count += 1
|
| 35 |
else: # 在本地開發環境中
|
| 36 |
db_path = os.path.join(app.instance_path, 'event.db')
|
| 37 |
os.makedirs(app.instance_path, exist_ok=True)
|
| 38 |
|
| 39 |
+
print(f"Using database path: {db_path}")
|
| 40 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 41 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 42 |
|
|
|
|
| 44 |
try:
|
| 45 |
if not os.path.exists(db_path):
|
| 46 |
open(db_path, 'a').close()
|
| 47 |
+
print(f"Created new database file at {db_path}")
|
| 48 |
os.chmod(db_path, 0o666)
|
| 49 |
+
print(f"Set permissions for database file at {db_path}")
|
| 50 |
except OSError as e:
|
| 51 |
print(f"Error setting up database: {e}")
|
| 52 |
|
|
|
|
| 122 |
data = request.json
|
| 123 |
if data.get('password') == ADMIN_PASSWORD:
|
| 124 |
session['admin_logged_in'] = True
|
| 125 |
+
return jsonify({'success': True, 'message': 'Login successful'}), 200
|
| 126 |
+
return jsonify({'success': False, 'error': 'Invalid password'}), 401
|
| 127 |
|
| 128 |
@app.route('/api/admin/settings', methods=['GET', 'POST'])
|
| 129 |
@admin_required
|
templates/admin_login.html
CHANGED
|
@@ -50,7 +50,7 @@
|
|
| 50 |
|
| 51 |
const data = await response.json();
|
| 52 |
|
| 53 |
-
if (
|
| 54 |
Swal.fire({
|
| 55 |
icon: 'success',
|
| 56 |
title: '登入成功!',
|
|
@@ -58,7 +58,7 @@
|
|
| 58 |
timer: 1500,
|
| 59 |
showConfirmButton: false
|
| 60 |
}).then(() => {
|
| 61 |
-
window.location.
|
| 62 |
});
|
| 63 |
} else {
|
| 64 |
Swal.fire({
|
|
@@ -68,6 +68,7 @@
|
|
| 68 |
});
|
| 69 |
}
|
| 70 |
} catch (error) {
|
|
|
|
| 71 |
Swal.fire({
|
| 72 |
icon: 'error',
|
| 73 |
title: '登入失敗',
|
|
|
|
| 50 |
|
| 51 |
const data = await response.json();
|
| 52 |
|
| 53 |
+
if (data.success) {
|
| 54 |
Swal.fire({
|
| 55 |
icon: 'success',
|
| 56 |
title: '登入成功!',
|
|
|
|
| 58 |
timer: 1500,
|
| 59 |
showConfirmButton: false
|
| 60 |
}).then(() => {
|
| 61 |
+
window.location.replace('/admin');
|
| 62 |
});
|
| 63 |
} else {
|
| 64 |
Swal.fire({
|
|
|
|
| 68 |
});
|
| 69 |
}
|
| 70 |
} catch (error) {
|
| 71 |
+
console.error('Login error:', error);
|
| 72 |
Swal.fire({
|
| 73 |
icon: 'error',
|
| 74 |
title: '登入失敗',
|