ichieh commited on
Commit
05b779c
·
1 Parent(s): cb94895

Fix permission issues and update database path handling

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +13 -2
  3. init_db.py +10 -0
Dockerfile CHANGED
@@ -2,9 +2,25 @@ FROM python:3.11-slim
2
 
3
  WORKDIR /code
4
 
 
 
 
 
 
 
5
  COPY ./requirements.txt /code/requirements.txt
6
  RUN pip install --no-cache-dir -r requirements.txt
7
 
 
8
  COPY . /code/
9
 
 
 
 
 
 
 
 
 
 
10
  CMD ["python", "app.py"]
 
2
 
3
  WORKDIR /code
4
 
5
+ # Install system dependencies
6
+ RUN apt-get update && apt-get install -y \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ # Copy requirements first to leverage Docker cache
11
  COPY ./requirements.txt /code/requirements.txt
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
14
+ # Copy the rest of the application
15
  COPY . /code/
16
 
17
+ # Create the instance directory and set permissions
18
+ RUN mkdir -p /code/instance && \
19
+ chmod -R 777 /code/instance && \
20
+ chmod -R 777 /code
21
+
22
+ # Initialize the database
23
+ RUN python init_db.py
24
+
25
+ # Run the application
26
  CMD ["python", "app.py"]
app.py CHANGED
@@ -1,9 +1,20 @@
1
  from flask import Flask, render_template, request, jsonify
2
  from flask_sqlalchemy import SQLAlchemy
3
  from datetime import datetime
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- app = Flask(__name__)
6
- app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///event.db'
7
  db = SQLAlchemy(app)
8
 
9
  class Table(db.Model):
 
1
  from flask import Flask, render_template, request, jsonify
2
  from flask_sqlalchemy import SQLAlchemy
3
  from datetime import datetime
4
+ import os
5
+
6
+ app = Flask(__name__, instance_relative_config=True)
7
+
8
+ # 設置資料庫路徑
9
+ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'event.db')
10
+ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
11
+
12
+ # 確保 instance 資料夾存在
13
+ try:
14
+ os.makedirs(app.instance_path)
15
+ except OSError:
16
+ pass
17
 
 
 
18
  db = SQLAlchemy(app)
19
 
20
  class Table(db.Model):
init_db.py CHANGED
@@ -1,7 +1,17 @@
1
  from app import app, db, Table
 
2
 
3
  def init_tables():
 
 
 
 
 
 
4
  with app.app_context():
 
 
 
5
  # 清空現有的桌子
6
  Table.query.delete()
7
 
 
1
  from app import app, db, Table
2
+ import os
3
 
4
  def init_tables():
5
+ # 確保 instance 資料夾存在
6
+ try:
7
+ os.makedirs(app.instance_path)
8
+ except OSError:
9
+ pass
10
+
11
  with app.app_context():
12
+ # 確保所有表格都被創建
13
+ db.create_all()
14
+
15
  # 清空現有的桌子
16
  Table.query.delete()
17