-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathquickstart.bat
More file actions
153 lines (138 loc) · 5.08 KB
/
Copy pathquickstart.bat
File metadata and controls
153 lines (138 loc) · 5.08 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@echo off
REM ============================================
REM ProtoForge Quick Start (Windows / yi jian qi dong)
REM Double-click to run: checks env, installs deps, starts server.
REM All lines below the restart marker display in Chinese correctly.
REM ============================================
REM FIXED: known cmd.exe bug -- after `chcp 65001`, the rest of the SAME
REM batch file is re-parsed with wrong byte offsets (UTF-8 multi-byte chars
REM get split), producing garbage errors like "'xxx' is not recognized as
REM an internal or external command". Workaround: restart the script so
REM cmd.exe re-reads the whole file under UTF-8.
REM NOTE: keep every line ABOVE `chcp 65001` pure ASCII (Chinese text here
REM is parsed under the legacy codepage and would garble/execute wrongly).
if "%~1"=="__utf8" goto main
chcp 65001 >nul
cmd /s /c ""%~f0" __utf8"
exit /b %errorlevel%
:main
title ProtoForge Quick Start
cd /d "%~dp0"
echo.
echo ======================================================
echo ProtoForge 一键启动 (Windows)
echo 物联网协议仿真与测试平台
echo ======================================================
echo.
REM Step 1: 检查 Python
echo [1/4] 检查 Python ...
where python >nul 2>&1
if errorlevel 1 (
echo.
echo [错误] 没有找到 Python,请先安装 Python 3.10+
echo 下载地址: https://www.python.org/downloads/
echo 安装时请勾选 "Add Python to PATH"
echo.
pause
exit /b 1
)
python --version
echo.
REM Step 2: 创建/检查虚拟环境
echo [2/4] 检查 Python 虚拟环境 ...
if exist "venv\Scripts\python.exe" (
echo 虚拟环境已存在,跳过创建
) else (
echo 创建虚拟环境 ...
python -m venv venv
if errorlevel 1 (
echo [错误] 创建虚拟环境失败
pause
exit /b 1
)
echo 虚拟环境创建成功
)
echo.
REM Step 3: 检查依赖是否已安装(通过检测 protoforge 包是否可导入)
echo [3/4] 检查 Python 依赖 ...
venv\Scripts\python.exe -c "import protoforge" >nul 2>&1
if errorlevel 1 (
echo 首次运行,正在安装依赖(可能需要几分钟)...
venv\Scripts\python.exe -m pip install --quiet --upgrade pip
venv\Scripts\pip.exe install -e ".[all]" >nul 2>&1
if errorlevel 1 (
echo 全部协议安装失败,尝试安装核心依赖...
venv\Scripts\pip.exe install -e .
if errorlevel 1 (
echo [错误] 依赖安装失败,请检查网络连接
echo 可尝试设置国内镜像: pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pause
exit /b 1
)
)
echo 依赖安装成功
) else (
echo 依赖已安装,跳过
)
echo.
REM Step 4: 确保 .env 配置正确(关键:生成稳定的 JWT_SECRET)
echo [4/4] 检查配置文件 ...
REM FIXED: 原写法 "set /pf" 是无效语法(应为 set /p),变量读不到,
REM 导致首次生成的 .env 里 JWT_SECRET 为空
set "JWT_SECRET_GEN="
venv\Scripts\python.exe -c "import secrets; print(secrets.token_urlsafe(32))" > "%TEMP%\pf_jwt_tmp" 2>nul
set /p JWT_SECRET_GEN=<"%TEMP%\pf_jwt_tmp"
del "%TEMP%\pf_jwt_tmp" >nul 2>&1
if exist ".env" goto check_env
echo 首次运行,生成配置文件 ...
REM 生成 .env,确保 JWT_SECRET 非空且持久
(
echo # ProtoForge 配置文件(由 quickstart.bat 自动生成)
echo PROTOFORGE_HOST=0.0.0.0
echo PROTOFORGE_PORT=8000
echo PROTOFORGE_DB_PATH=data/protoforge.db
echo PROTOFORGE_JWT_SECRET=%JWT_SECRET_GEN%
echo PROTOFORGE_ADMIN_PASSWORD=admin
echo PROTOFORGE_DEMO_MODE=true
echo PROTOFORGE_LOG_LEVEL=info
echo PROTOFORGE_CORS_ORIGINS=http://localhost:8000,http://127.0.0.1:8000
) > ".env"
echo 配置文件已生成
goto env_done
:check_env
REM 检查 .env 中 JWT_SECRET 是否为空,为空则补填
REM (用两个 findstr 串联:先取 JWT_SECRET 行,再匹配行尾为空的情形)
findstr /B /C:"PROTOFORGE_JWT_SECRET=" .env 2>nul | findstr /R /C:"PROTOFORGE_JWT_SECRET=$" >nul 2>&1
if not errorlevel 1 (
echo 检测到 JWT_SECRET 为空,正在修复 ...
powershell -NoProfile -Command "(Get-Content '.env') -replace '^PROTOFORGE_JWT_SECRET=$', 'PROTOFORGE_JWT_SECRET=%JWT_SECRET_GEN%' | Set-Content '.env'"
echo JWT_SECRET 已修复
) else (
echo 配置文件正常
)
:env_done
echo.
REM 确保 data 目录存在
if not exist "data" mkdir data
REM 显示启动信息
echo ======================================================
echo.
echo ProtoForge 正在启动...
echo.
echo 浏览器打开: http://localhost:8000
echo 登录账号: admin
echo 登录密码: admin
echo.
echo 按 Ctrl+C 可停止服务
echo.
echo ======================================================
echo.
REM 启动服务(演示模式)
venv\Scripts\python.exe -m protoforge.cli demo
if errorlevel 1 (
echo.
echo [错误] 服务启动失败,请检查上方错误信息
echo 或尝试重新运行本脚本
echo.
pause
)