Yukuiii commited on
Commit
8e5d415
·
1 Parent(s): c3f9121

fix: 尝试修复无法写入

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -2
  2. app.py +14 -4
Dockerfile CHANGED
@@ -36,8 +36,7 @@ COPY --chown=appuser:appuser . .
36
  # 创建可写目录并设置权限
37
  RUN mkdir -p /app/data && \
38
  chown -R appuser:appuser /app && \
39
- chmod 755 /app && \
40
- chmod 664 /app/config.json # 显式设置配置文件权限
41
 
42
  # 切换用户
43
  USER appuser
 
36
  # 创建可写目录并设置权限
37
  RUN mkdir -p /app/data && \
38
  chown -R appuser:appuser /app && \
39
+ chmod 775 -R /app/data # 重点修改这里,添加递归权限设置
 
40
 
41
  # 切换用户
42
  USER appuser
app.py CHANGED
@@ -53,12 +53,22 @@ active_accounts = set()
53
  # (1) 配置文件的读写函数
54
  # -------------------------------------------------------------------
55
  current_dir = os.path.dirname(os.path.abspath(__file__))
56
- CONFIG_PATH = os.path.join(current_dir, "data", "config.json") # 使用子目录
57
 
58
- # 启动时检查并创建配置文件
 
 
 
 
 
 
 
59
  if not os.path.exists(CONFIG_PATH):
60
- with open(CONFIG_PATH, "w") as f:
61
- json.dump({"keys": [], "accounts": []}, f)
 
 
 
 
62
 
63
  def load_config():
64
  """从 config.json 加载配置,出错则返回空 dict"""
 
53
  # (1) 配置文件的读写函数
54
  # -------------------------------------------------------------------
55
  current_dir = os.path.dirname(os.path.abspath(__file__))
 
56
 
57
+ # 确保目录存在且可写
58
+ DATA_DIR = os.path.join(current_dir, "data")
59
+ if not os.path.exists(DATA_DIR):
60
+ os.makedirs(DATA_DIR, exist_ok=True, mode=0o775) # 显式设置目录权限
61
+
62
+ CONFIG_PATH = os.path.join(DATA_DIR, "config.json")
63
+
64
+ # 首次运行时创建配置文件
65
  if not os.path.exists(CONFIG_PATH):
66
+ try:
67
+ with open(CONFIG_PATH, "w") as f:
68
+ json.dump({"keys": [], "accounts": []}, f)
69
+ os.chmod(CONFIG_PATH, 0o664) # 显式设置文件权限
70
+ except Exception as e:
71
+ app.logger.error(f"初始化配置文件失败: {e}")
72
 
73
  def load_config():
74
  """从 config.json 加载配置,出错则返回空 dict"""