主要功能更新: - 新增配置管理模块:支持YAML配置文件的在线编辑、验证和保存 - 实现评分规则管理:支持YAML格式评分配置,简化条件配置流程 - 完善前后端通信:优化API路径配置,解决跨域和网络连接问题 - 新增组件架构:AppContent、ConfigManager、ScoringRules、ThreatGraph等核心组件 - 集成js-yaml库:支持前端YAML格式解析和编辑 - 修复UI渲染问题:解决Naive UI组件空值引用和布局问题 技术改进: - 动态API地址配置,支持不同网络环境 - 增强错误处理和空值保护 - 优化表格布局和操作按钮排列 - 完善类型定义和接口规范 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
116 lines
4.0 KiB
Python
116 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
AI SOC Analyst Backend - One-Click Start Script
|
|
自动化启动脚本:停止端口占用、管理虚拟环境、安装依赖、启动后端
|
|
Run with: python3 run_backend.py
|
|
"""
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import signal
|
|
from pathlib import Path
|
|
|
|
class BackendLauncher:
|
|
def __init__(self):
|
|
self.project_root = Path(__file__).parent
|
|
self.backend_path = self.project_root / "backend"
|
|
self.venv_path = self.project_root / "venv"
|
|
self.port = 8000
|
|
|
|
def kill_port(self, port):
|
|
"""停止指定端口上的进程"""
|
|
print(f"🔍 检查端口 {port} 上的进程...")
|
|
try:
|
|
result = subprocess.run(['lsof', '-ti', f':{port}'],
|
|
capture_output=True, text=True)
|
|
if result.stdout.strip():
|
|
pids = result.stdout.strip().split('\n')
|
|
for pid in pids:
|
|
if pid:
|
|
print(f"🔥 停止进程 PID: {pid}")
|
|
os.kill(int(pid), signal.SIGTERM)
|
|
|
|
except (subprocess.CalledProcessError, ProcessLookupError, ValueError):
|
|
pass
|
|
|
|
def setup_venv(self):
|
|
"""设置虚拟环境"""
|
|
if not self.venv_path.exists():
|
|
print("🔧 创建虚拟环境...")
|
|
subprocess.run([sys.executable, '-m', 'venv', str(self.venv_path)])
|
|
|
|
venv_python = self.venv_path / "bin" / "python"
|
|
if not venv_python.exists():
|
|
print("❌ 虚拟环境创建失败")
|
|
sys.exit(1)
|
|
|
|
print("✅ 虚拟环境就绪")
|
|
return venv_python
|
|
|
|
def install_dependencies(self, venv_python):
|
|
"""安装Python依赖"""
|
|
requirements_files = [
|
|
self.backend_path / "requirements.txt",
|
|
self.project_root / "requirements.txt"
|
|
]
|
|
|
|
for req_file in requirements_files:
|
|
if req_file.exists():
|
|
print(f"📦 安装依赖: {req_file}")
|
|
subprocess.run([str(venv_python), '-m', 'pip', 'install', '-r', str(req_file)])
|
|
break
|
|
else:
|
|
print("🔧 安装基础依赖...")
|
|
base_deps = ['uvicorn', 'fastapi', 'python-multipart']
|
|
subprocess.run([str(venv_python), '-m', 'pip', 'install'] + base_deps)
|
|
|
|
def start_backend(self, venv_python):
|
|
"""启动后端服务"""
|
|
print("🚀 启动 AI SOC 分析师后端...")
|
|
print(f"📍 后端API地址: http://localhost:{self.port}")
|
|
print(f"📖 API文档地址: http://localhost:{self.port}/docs")
|
|
print("⚡ 按 Ctrl+C 停止服务\n")
|
|
|
|
# 添加backend目录到Python路径
|
|
env = os.environ.copy()
|
|
pythonpath = str(self.backend_path)
|
|
if 'PYTHONPATH' in env:
|
|
pythonpath = f"{env['PYTHONPATH']}:{pythonpath}"
|
|
env['PYTHONPATH'] = pythonpath
|
|
|
|
try:
|
|
# 使用虚拟环境的Python启动uvicorn
|
|
subprocess.run([
|
|
str(venv_python), '-m', 'uvicorn',
|
|
'app.main:app',
|
|
'--host', '0.0.0.0',
|
|
'--port', str(self.port),
|
|
'--reload',
|
|
'--log-level', 'info'
|
|
], cwd=str(self.backend_path), env=env)
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n🛑 后端服务已停止")
|
|
|
|
def run(self):
|
|
"""执行完整的启动流程"""
|
|
try:
|
|
# 1. 停止端口占用
|
|
self.kill_port(self.port)
|
|
|
|
# 2. 设置虚拟环境
|
|
venv_python = self.setup_venv()
|
|
|
|
# 3. 安装依赖
|
|
self.install_dependencies(venv_python)
|
|
|
|
# 4. 启动后端
|
|
self.start_backend(venv_python)
|
|
|
|
except Exception as e:
|
|
print(f"❌ 启动失败: {e}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
launcher = BackendLauncher()
|
|
launcher.run() |