diff --git a/run.md b/run.md index 1dbeb0f..ed3f1c8 100644 --- a/run.md +++ b/run.md @@ -11,7 +11,42 @@ ## 本地开发启动 -### 1. 启动后端服务 +后端服务和 frp 隧道已配置为 macOS launchd 系统级服务,开机自动启动,崩溃自动重启,与 IDE 无关。 + +### 系统级服务(launchd) + +已创建两个 LaunchAgent: + +| 服务 | plist | 日志 | +|------|-------|------| +| 后端 (localhost:3333) | `~/Library/LaunchAgents/com.sbrain.backend.plist` | `~/Library/Logs/sbrain-backend.log` | +| frp 隧道 (13333→3333) | `~/Library/LaunchAgents/com.frp.client.plist` | `~/Library/Logs/frpc.log` | + +两个服务均配置了 `KeepAlive: true`,崩溃后自动重启。开机/登录后自动启动,关闭 IDE 和终端不影响运行。 + +#### 管理命令 + +```bash +# 停止服务 +launchctl unload ~/Library/LaunchAgents/com.sbrain.backend.plist +launchctl unload ~/Library/LaunchAgents/com.frp.client.plist + +# 启动服务 +launchctl load ~/Library/LaunchAgents/com.sbrain.backend.plist +launchctl load ~/Library/LaunchAgents/com.frp.client.plist + +# 查看日志 +tail -f ~/Library/Logs/sbrain-backend.log +tail -f ~/Library/Logs/frpc.log + +# 检查状态 +curl -s http://localhost:3333/api/health +pgrep -l frpc +``` + +### 手动启动(备用,launchd 未运行时) + +#### 1. 启动后端服务 ```bash cd server && npm run dev @@ -19,7 +54,7 @@ cd server && npm run dev 后端运行在 `http://localhost:3333`,连接本地 PostgreSQL(`localhost:5432`,数据库 `bill_query`)。 -### 2. 启动反向隧道(线上前端访问本地后端) +#### 2. 启动 frp 隧道 线上 Nginx 将 `/api/` 代理到远程 `127.0.0.1:13333`,通过 frp tcp 代理转发到本地后端 3333 端口: @@ -34,20 +69,20 @@ localPort = 3333 remotePort = 13333 ``` -重启 frpc 即可: +重启 frpc: ```bash kill -9 $(pgrep frpc); cd /Users/freedak/frp && ./frpc -c frpc.toml & ``` -### 3. 访问 +### 访问 -- **线上前端**:`https://dm.all8ai.top`(API请求通过隧道转发到本地后端) +- **线上前端**:`https://dm.all8ai.top`(API请求通过 frp 隧道转发到本地后端) - **本地前端开发**:`cd client && npm run dev`,访问 `http://localhost:5173`(Vite proxy 自动转发 `/api` 到 `localhost:3333`) ### 注意事项 - 本地 PostgreSQL 端口 5432 被 Homebrew PostgreSQL 占用,数据库 `bill_query` 在本地 -- 反向隧道将远程 13333 映射到本地 3333,服务器 Nginx 配置 `/api/` → `127.0.0.1:13333` -- 隧道断开后线上前端 API 会失效,需重新执行隧道命令 +- frp 隧道将远程 13333 映射到本地 3333,服务器 Nginx 配置 `/api/` → `127.0.0.1:13333` +- launchd 服务开机自启,关闭 IDE/终端不影响;仅注销/关机时停止,下次登录/开机自动恢复 - `server/.env` 中 `DB_PORT=5432` 连接本地数据库,不要改为 5433 \ No newline at end of file