From ed91df6607d5649632b31101226802e5b6f445d5 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Thu, 28 Nov 2024 13:22:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E5=8C=85=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.py | 7 ------- scripts/build_win.ps1 | 2 ++ src/app.py | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/run.py b/run.py index 4a56ece..cb7d763 100644 --- a/run.py +++ b/run.py @@ -12,21 +12,14 @@ def main(): os.makedirs(config.LOG_DIR, exist_ok=True) os.makedirs(config.DATA_DIR, exist_ok=True) - # 创建并运行应用 app = create_app() - - logger.info(f"Starting server in {'debug' if config.FLASK_DEBUG else 'production'} mode") - logger.info(f"Server will run on {config.FLASK_HOST}:{config.FLASK_PORT}") - app.run( host=config.FLASK_HOST, port=config.FLASK_PORT, debug=config.FLASK_DEBUG ) - except Exception as e: logger.error(f"Error starting application: {str(e)}") - logger.error("Detailed traceback:", exc_info=True) raise if __name__ == '__main__': diff --git a/scripts/build_win.ps1 b/scripts/build_win.ps1 index 83a0894..bf78e6e 100644 --- a/scripts/build_win.ps1 +++ b/scripts/build_win.ps1 @@ -19,6 +19,8 @@ Write-Host "Building frontend..." Push-Location frontend npm install npm run build +Copy-Item dist/* . -Recurse -Force +Remove-Item dist -Recurse -Force Pop-Location # Package with PyInstaller diff --git a/src/app.py b/src/app.py index e9fcbea..319adc9 100644 --- a/src/app.py +++ b/src/app.py @@ -1,9 +1,11 @@ from flask import Flask from flask_cors import CORS +from flask import send_from_directory from .routes import api_bp from .logger import setup_logger from config import config import os +import sys logger = setup_logger(__name__) @@ -23,10 +25,35 @@ def create_app(): app.register_blueprint(api_bp, url_prefix='/api') logger.info("API blueprint registered") - # 记录配置信息 - logger.info(f"Database: {app.config['MYSQL_DB']} on {app.config['MYSQL_HOST']}") - logger.info(f"Server will run on {config.FLASK_HOST}:{config.FLASK_PORT}") - logger.info(f"Debug mode: {config.FLASK_DEBUG}") + # 获取前端文件路径 + if getattr(sys, 'frozen', False): + # PyInstaller 打包后的路径 + frontend_path = os.path.join(sys._MEIPASS, 'frontend') + logger.info(f"Running in frozen mode, frontend path: {frontend_path}") + logger.info(f"MEIPASS path: {sys._MEIPASS}") + logger.info(f"Files in frontend dir: {os.listdir(frontend_path)}") + else: + # 开发环境路径 + frontend_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'frontend', 'dist') + + # 服务前端文件 + @app.route('/', defaults={'path': ''}) + @app.route('/') + def serve_frontend(path): + logger.info(f"Serving path: {path}") + logger.info(f"Frontend path: {frontend_path}") + logger.info(f"Full file path: {os.path.join(frontend_path, path)}") + logger.info(f"File exists: {os.path.exists(os.path.join(frontend_path, path))}") + try: + if path == "": + return send_from_directory(frontend_path, 'index.html') + file_path = os.path.join(frontend_path, path) + if os.path.exists(file_path): + return send_from_directory(frontend_path, path) + return send_from_directory(frontend_path, 'index.html') + except Exception as e: + logger.error(f"Error serving file {path}: {str(e)}") + return str(e), 500 return app