@@ -24,9 +24,15 @@ def __init__(self):
2424 # 配置跨域请求
2525 CORS (self .app )
2626
27+ # 使用绝对路径以避免 CWD (当前工作目录) 不同导致的问题
28+ # .../DogApp2/dog
29+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
30+ # .../DogApp2
31+ root_dir = os .path .dirname (current_dir )
32+
2733 # 服务器配置
28- self .UPLOAD_FOLDER = 'uploads'
29- self .ASSETS_FOLDER = os .path .join ('dog' , 'assets' )
34+ self .UPLOAD_FOLDER = os . path . join ( root_dir , 'uploads' )
35+ self .ASSETS_FOLDER = os .path .join (current_dir , 'assets' )
3036 self .ALLOWED_IMAGE_EXTENSIONS = {'png' , 'jpg' , 'jpeg' , 'gif' , 'bmp' }
3137 self .MAX_FILE_SIZE = 16 * 1024 * 1024 # 16MB
3238
@@ -80,8 +86,8 @@ def __init__(self):
8086 self .notification_history_lock = Lock ()
8187 self .notification_next_id = 1
8288
83- # 简单账号存储(明文,保存在 uploads /users.json)
84- self .users_filename = 'users.json'
89+ # 简单账号存储(明文,保存在 dog /users.json)
90+ self .users_file_path = os . path . join ( current_dir , 'users.json' )
8591
8692 # 日程提醒推送相关
8793 self .schedule_notifier_stop = threading .Event ()
@@ -152,17 +158,28 @@ def save_assets_json(self, data, filename):
152158
153159 # ========== 简易账号存储(明文,仅演示用途) ==========
154160 def load_users (self ):
155- data = self .load_json_data (self .users_filename )
156- if not data :
157- return {'users' : []}
158- if isinstance (data , list ):
159- return {'users' : data }
160- if isinstance (data , dict ) and 'users' in data :
161- return {'users' : data .get ('users' , [])}
161+ try :
162+ with self .file_lock :
163+ if os .path .exists (self .users_file_path ):
164+ with open (self .users_file_path , 'r' , encoding = 'utf-8' ) as f :
165+ data = json .load (f )
166+ if isinstance (data , list ):
167+ return {'users' : data }
168+ if isinstance (data , dict ) and 'users' in data :
169+ return {'users' : data .get ('users' , [])}
170+ except Exception as e :
171+ logger .error (f"加载用户数据失败: { e } " )
162172 return {'users' : []}
163173
164174 def save_users (self , users ):
165- return self .save_json_data (users , self .users_filename )
175+ try :
176+ with self .file_lock :
177+ with open (self .users_file_path , 'w' , encoding = 'utf-8' ) as f :
178+ json .dump (users , f , ensure_ascii = False , indent = 4 )
179+ return True
180+ except Exception as e :
181+ logger .error (f"保存用户数据失败: { e } " )
182+ return False
166183
167184 def get_video_stream_generator (self , video_path ):
168185 """生成视频流,循环播放"""
@@ -1732,21 +1749,21 @@ def internal_error(e):
17321749
17331750
17341751
1735- def run_server (self ):
1752+ def run_server (self , port = 5000 ):
17361753 """运行Flask服务器"""
17371754 print ("启动机器狗控制服务器..." )
17381755 print (f"上传目录: { os .path .abspath (self .app .config ['UPLOAD_FOLDER' ])} " )
1739- print ("服务器地址: http://0.0.0.0:5000 " )
1756+ print (f "服务器地址: http://0.0.0.0:{ port } " )
17401757
17411758 # 使用多线程模式运行
1742- self .app .run (host = '0.0.0.0' , port = 5000 , debug = False , threaded = True )
1759+ self .app .run (host = '0.0.0.0' , port = port , debug = False , threaded = True )
17431760
1744- def start (self ):
1761+ def start (self , port = 5000 ):
17451762 """启动服务器线程"""
1746- server_thread = threading .Thread (target = self .run_server )
1763+ server_thread = threading .Thread (target = self .run_server , kwargs = { 'port' : port } )
17471764 server_thread .daemon = True
17481765 server_thread .start ()
1749- print ("机器狗API服务器已启动在端口5000 " )
1766+ print (f"机器狗API服务器已启动在端口 { port } " )
17501767
17511768# 集成到现有的机器狗主程序
17521769def main ():
0 commit comments