chore: 为 Admin 启动脚本增加热重载 (#13)

This commit is contained in:
chengma
2026-08-06 17:34:56 +08:00
parent 5a5c1f1f68
commit 7a6c647eda
4 changed files with 95 additions and 2 deletions
+2
View File
@@ -10,6 +10,8 @@ dist/
*.spec
admin/admin.exe
admin/admin
admin/admin-dev.exe
admin/tmp/
# Excel 打开时生成的锁文件
~$*.xlsx
+24
View File
@@ -0,0 +1,24 @@
root = "."
tmp_dir = "tmp"
[build]
cmd = "go build -o ./admin-dev.exe ."
bin = "admin-dev.exe"
include_ext = ["go", "html", "css", "js"]
exclude_dir = ["data", "tmp", "testdata"]
exclude_regex = ["_test\\.go"]
exclude_unchanged = true
delay = 500
stop_on_error = true
send_interrupt = false
kill_delay = "500ms"
[log]
time = true
[misc]
clean_on_exit = true
[screen]
clear_on_rebuild = false
keep_scroll = true
+14 -2
View File
@@ -67,6 +67,20 @@ go get github.com/xuri/excelize/v2@v2.9.1
## 3. 跑起来
推荐在仓库根目录运行热重载脚本:
```powershell
cd D:\chengma\cmautobuy
.\run_admin.bat
```
脚本第一次运行时会自动安装与 Go 1.23 兼容的
`github.com/air-verse/air@v1.61.7`,以后会直接使用已安装的固定版本。
保存 Go、HTML、CSS 或 JavaScript 文件后,Air 会自动重新编译并重启 Admin;
按 `Ctrl+C` 停止。
如果只想临时运行一次、不需要热重载,也可以:
```powershell
cd D:\chengma\cmautobuy\admin
go run .
@@ -76,8 +90,6 @@ go run .
**这个命令是安全的**:Admin 只管理数据,不会连手机、不会下单。放心随便跑。
改了代码要重启才生效(Go 不像 Python 那样改完就生效)。按 `Ctrl+C` 停掉再 `go run .`。
## 4. 你应该看到什么
左边(或顶部)是四个模块的导航:
+55
View File
@@ -0,0 +1,55 @@
@echo off
setlocal
set "AIR_VERSION=v1.61.7"
cd /d "%~dp0admin"
if errorlevel 1 (
echo [ERROR] Cannot enter the admin directory.
pause
exit /b 1
)
where go >nul 2>&1
if errorlevel 1 (
echo [ERROR] Go was not found. Install Go 1.23.0 and reopen the terminal.
pause
exit /b 1
)
for /f "usebackq delims=" %%G in (`go env GOPATH`) do set "ADMIN_GOPATH=%%G"
if not defined ADMIN_GOPATH (
echo [ERROR] Cannot read GOPATH.
pause
exit /b 1
)
set "AIR_EXE=%ADMIN_GOPATH%\bin\air.exe"
set "NEED_INSTALL=0"
if not exist "%AIR_EXE%" set "NEED_INSTALL=1"
if exist "%AIR_EXE%" (
"%AIR_EXE%" -v 2>nul | findstr /c:"%AIR_VERSION%" >nul
if errorlevel 1 set "NEED_INSTALL=1"
)
if "%NEED_INSTALL%"=="1" (
echo [INFO] Installing Air %AIR_VERSION% for Go 1.23...
go install github.com/air-verse/air@%AIR_VERSION%
if errorlevel 1 (
echo [ERROR] Air installation failed. Check the network and GOPROXY.
pause
exit /b 1
)
)
echo [INFO] Starting Admin with hot reload. Press Ctrl+C to stop.
"%AIR_EXE%" -c .air.toml
set "ADMIN_EXIT_CODE=%ERRORLEVEL%"
if not "%ADMIN_EXIT_CODE%"=="0" (
echo [ERROR] Admin stopped with exit code %ADMIN_EXIT_CODE%.
pause
)
endlocal & exit /b %ADMIN_EXIT_CODE%