修改打包配置
This commit is contained in:
parent
047cafa7c7
commit
ed91df6607
7
run.py
7
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__':
|
||||
|
||||
@ -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
|
||||
|
||||
35
src/app.py
35
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('/<path:path>')
|
||||
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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user