Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
ef1f31f
1
Parent(s): 9f7a6e2
Update to use persistent storage for database
Browse files- Dockerfile +4 -7
- app.py +27 -10
Dockerfile
CHANGED
|
@@ -18,16 +18,13 @@ COPY . /code/
|
|
| 18 |
RUN useradd -m myuser && \
|
| 19 |
chown -R myuser:myuser /code
|
| 20 |
|
| 21 |
-
# Create
|
| 22 |
-
RUN mkdir -p /
|
| 23 |
-
chown -R myuser:myuser /
|
| 24 |
-
chmod
|
| 25 |
|
| 26 |
# Switch to non-root user
|
| 27 |
USER myuser
|
| 28 |
|
| 29 |
-
# Initialize the database
|
| 30 |
-
RUN python init_db.py
|
| 31 |
-
|
| 32 |
# Run the application
|
| 33 |
CMD ["python", "app.py"]
|
|
|
|
| 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 myuser:myuser /data && \
|
| 24 |
+
chmod 777 /data
|
| 25 |
|
| 26 |
# Switch to non-root user
|
| 27 |
USER myuser
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# Run the application
|
| 30 |
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -12,17 +12,21 @@ app.secret_key = os.urandom(24)
|
|
| 12 |
# 設置管理者密碼(從環境變數獲取)
|
| 13 |
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', '1234') # 預設密碼為 1234
|
| 14 |
|
| 15 |
-
# 設置資料庫路徑
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 18 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 19 |
|
| 20 |
-
# 確保
|
| 21 |
try:
|
| 22 |
-
os.makedirs(app.instance_path, exist_ok=True)
|
| 23 |
if not os.path.exists(db_path):
|
| 24 |
open(db_path, 'a').close()
|
| 25 |
-
os.chmod(app.instance_path, 0o777)
|
| 26 |
os.chmod(db_path, 0o666)
|
| 27 |
except OSError as e:
|
| 28 |
print(f"Error setting up database: {e}")
|
|
@@ -42,11 +46,24 @@ class Participant(db.Model):
|
|
| 42 |
table_id = db.Column(db.Integer, db.ForeignKey('table.id'))
|
| 43 |
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# 管理者認證裝飾器
|
| 52 |
def admin_required(f):
|
|
|
|
| 12 |
# 設置管理者密碼(從環境變數獲取)
|
| 13 |
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', '1234') # 預設密碼為 1234
|
| 14 |
|
| 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 |
|
| 26 |
+
# 確保資料庫文件存在並設置權限
|
| 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}")
|
|
|
|
| 46 |
table_id = db.Column(db.Integer, db.ForeignKey('table.id'))
|
| 47 |
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
| 48 |
|
| 49 |
+
# 初始化資料庫和創建預設桌子
|
| 50 |
+
def init_db():
|
| 51 |
+
with app.app_context():
|
| 52 |
+
try:
|
| 53 |
+
db.create_all()
|
| 54 |
+
# 只在沒有桌子時創建預設桌子
|
| 55 |
+
if Table.query.count() == 0:
|
| 56 |
+
for i in range(1, 11): # 預設創建10張桌子
|
| 57 |
+
table = Table(capacity=8, name=f'Table {i}')
|
| 58 |
+
db.session.add(table)
|
| 59 |
+
db.session.commit()
|
| 60 |
+
print("Default tables created successfully!")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error initializing database: {e}")
|
| 63 |
+
db.session.rollback()
|
| 64 |
+
|
| 65 |
+
# 初始化資料庫
|
| 66 |
+
init_db()
|
| 67 |
|
| 68 |
# 管理者認證裝飾器
|
| 69 |
def admin_required(f):
|