Files
worklist/start.py
bluish 6ecd95ad5d 将框架从 Flask 迁移到 FastAPI
主要改动:
- 更新依赖:使用 fastapi、uvicorn 替代 Flask
- 数据库层:从 Flask-SQLAlchemy 迁移到纯 SQLAlchemy
- 新增 database.py 管理数据库连接和会话
- 路由层:从 Blueprint 迁移到 APIRouter,添加 Pydantic 模型验证
- 应用层:使用 FastAPI 中间件替代 Flask 插件
- 启动方式:使用 uvicorn 替代 Flask 开发服务器
- 更新 Docker 配置以支持 FastAPI

优势:
- 更高的性能和异步支持
- 自动生成 OpenAPI 文档
- 更好的类型安全和数据验证
- 所有 API 端点保持向后兼容

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-31 10:12:22 +00:00

96 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
工作任务管理系统启动脚本
"""
import os
import sys
import subprocess
import webbrowser
import time
from pathlib import Path
def check_python_version():
"""检查Python版本"""
if sys.version_info < (3, 7):
print("错误: 需要Python 3.7或更高版本")
sys.exit(1)
def install_dependencies():
"""安装依赖"""
print("正在安装Python依赖...")
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "backend/requirements.txt"])
print("✓ Python依赖安装完成")
except subprocess.CalledProcessError as e:
print(f"错误: 安装依赖失败 - {e}")
sys.exit(1)
def create_env_file():
"""创建环境变量文件"""
env_file = Path("backend/.env")
if not env_file.exists():
print("创建环境变量文件...")
with open(env_file, "w", encoding="utf-8") as f:
f.write("""# OpenAI API配置可选用于AI润色功能
OPENAI_API_KEY=your_openai_api_key_here
# 数据库配置
DATABASE_URL=sqlite:///worklist.db
# FastAPI配置
SECRET_KEY=your-secret-key-here
""")
print("✓ 环境变量文件已创建: backend/.env")
print("提示: 如需使用AI润色功能请在.env文件中设置OPENAI_API_KEY")
def start_server():
"""启动服务器"""
print("正在启动服务器...")
os.chdir("backend")
try:
# 启动FastAPI应用 (使用uvicorn)
subprocess.run([sys.executable, "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "5000", "--reload"])
except KeyboardInterrupt:
print("\n服务器已停止")
except Exception as e:
print(f"错误: 启动服务器失败 - {e}")
sys.exit(1)
def main():
"""主函数"""
print("=" * 50)
print("工作任务管理系统")
print("=" * 50)
# 检查Python版本
check_python_version()
# 安装依赖
install_dependencies()
# 创建环境变量文件
create_env_file()
print("\n启动说明:")
print("1. 服务器启动后,请在浏览器中访问: http://localhost:5000")
print("2. 按 Ctrl+C 停止服务器")
print("3. 如需使用AI润色功能请配置OpenAI API密钥")
print("\n正在启动服务器...")
# 延迟2秒后自动打开浏览器
def open_browser():
time.sleep(2)
webbrowser.open("http://localhost:5000")
import threading
browser_thread = threading.Thread(target=open_browser)
browser_thread.daemon = True
browser_thread.start()
# 启动服务器
start_server()
if __name__ == "__main__":
main()