-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_all.ps1
More file actions
108 lines (94 loc) · 4.58 KB
/
Copy pathstart_all.ps1
File metadata and controls
108 lines (94 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
$ErrorActionPreference='Stop'
$ROOT='D:\wei\ec3'
$FE="$ROOT\EduAdmin"
$BE="$FE\server"
$VENV="$ROOT\.venv"
$LOGDIR="$ROOT\logs"
$FELOG="$LOGDIR\frontend.log"
$BELOG="$LOGDIR\backend.log"
New-Item -ItemType Directory -Force -Path $LOGDIR | Out-Null
function Log($m){ "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $m" }
function Health($url,$retry=30){
for($i=1;$i -le $retry;$i++){
try { Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 3 | Out-Null; return $true } catch { Start-Sleep -Milliseconds 500 }
}; return $false
}
# 1) 前端优先启动
Log "检查 Node/npm..."
$nodeOk = $false
try { (Get-Command node -ErrorAction Stop) | Out-Null; (Get-Command npm -ErrorAction Stop) | Out-Null; $nodeOk = $true } catch { $nodeOk = $false }
if(-not $nodeOk){
Log "未检测到 Node,尝试用 winget 安装 Node LTS..."
winget install --id OpenJS.NodeJS.LTS -e --source winget --silent --accept-source-agreements --accept-package-agreements | Out-Null
Start-Sleep -Seconds 5
}
Set-Location $FE
if(-not (Test-Path "node_modules")){
Log "前端依赖安装开始..."
npm install *> $FELOG 2>&1
Log "前端依赖安装完成,日志:$FELOG"
}else{ Log "前端依赖已存在,跳过安装。" }
try{
$cfg = Get-Content -Raw "vue.config.js"
if($cfg -match "edu\.todear\.net"){
($cfg -replace "http://edu\.todear\.net:8990/api","http://127.0.0.1:8000/api") | Set-Content "vue.config.js"
Log "已将前端代理切换到 http://127.0.0.1:8000/api"
}
}catch{}
Log "启动前端 dev server(后台)..."
$feCmd = "npm run dev"
Start-Process -WindowStyle Hidden -FilePath powershell -ArgumentList "-NoProfile","-ExecutionPolicy","Bypass","-Command",$feCmd -RedirectStandardOutput $FELOG -RedirectStandardError $FELOG
Start-Sleep -Seconds 2
Log "等待前端 http://127.0.0.1:9527 可用..."
if(-not (Health "http://127.0.0.1:9527" 60)){
Log "前端未在预期时间就绪,打印最近 100 行日志:"
Get-Content $FELOG -Tail 100
throw "前端启动失败,请先修复前端日志中的错误"
}
Log "前端已就绪:http://127.0.0.1:9527 (日志:$FELOG)"
# 2) 后端启动
function Find-Py(){
$cands=@(
"$env:LOCALAPPDATA\Programs\Python\Python312\python.exe",
"C:\\Program Files\\Python312\\python.exe",
"C:\\Program Files (x86)\\Python312\\python.exe"
)
foreach($p in $cands){ if(Test-Path $p){ return $p } }
$pc=(Get-Command python -ErrorAction SilentlyContinue); if($pc){ return $pc.Source }
return $null
}
$py = Find-Py
if(-not $py){
Log "未检测到 Python,尝试安装 Python 3.12..."
winget install --id Python.Python.3.12 -e --source winget --silent --accept-source-agreements --accept-package-agreements | Out-Null
Start-Sleep -Seconds 5
$py = Find-Py
if(-not $py){ throw "Python 未安装成功,请查看 winget 输出" }
}
Log "使用 Python:$py"
if(-not (Test-Path $VENV)){ & $py -m venv $VENV }
& "$VENV\Scripts\python.exe" -m pip install -U pip setuptools wheel *> $BELOG 2>&1
& "$VENV\Scripts\python.exe" -m pip install -r "$BE\requirements.txt" *> $BELOG 2>&1
New-Item -ItemType Directory -Force -Path "$BE\data","$BE\data\uploads" | Out-Null
Log "启动后端(Uvicorn,后台)..."
$beCmd="`"$VENV\Scripts\python.exe`" -m uvicorn app.main:app --reload --app-dir `"$BE`" --port 8000"
Start-Process -WindowStyle Hidden -FilePath powershell -ArgumentList "-NoProfile","-ExecutionPolicy","Bypass","-Command",$beCmd -RedirectStandardOutput $BELOG -RedirectStandardError $BELOG
Start-Sleep -Seconds 2
Log "等待后端 http://127.0.0.1:8000/health 可用..."
if(-not (Health "http://127.0.0.1:8000/health" 60)){
Log "后端未在预期时间就绪,打印最近 100 行日志:"
Get-Content $BELOG -Tail 100
Log "尝试自动下载 NLTK 资源后重启后端..."
& "$VENV\Scripts\python.exe" -c "import nltk;\n\nfor pkg in ('punkt','stopwords'):\n\ttry:\n\t\tbase=('tokenizers','corpora')[pkg=='stopwords']\n\t\tnltk.data.find(base+'/'+pkg)\n\texcept LookupError:\n\t\tnltk.download(pkg, quiet=True)\nprint('NLTK ready')"
Get-Process -Name "uvicorn" -ErrorAction SilentlyContinue | Stop-Process -Force
Start-Process -WindowStyle Hidden -FilePath powershell -ArgumentList "-NoProfile","-ExecutionPolicy","Bypass","-Command",$beCmd -RedirectStandardOutput $BELOG -RedirectStandardError $BELOG
if(-not (Health "http://127.0.0.1:8000/health" 60)){
Log "后端仍未就绪,打印完整日志路径:$BELOG"
Get-Content $BELOG -Tail 200
throw "后端启动失败,请根据日志修复"
}
}
Log "后端已就绪:http://127.0.0.1:8000 (日志:$BELOG)"
Start-Process "http://localhost:9527/autograde.html"
Log "ALL READY"