Files
SBrainCO/run.md
T
freedakgmail c3fbe4ab81 feat: SaaS架构改造 - 后端部署到服务器, 数据库留本机用frp隧道连接
- 新增 deploy-server.sh: 后端部署脚本(rsync + npm install + pm2)
- frp 隧道从暴露API(3333→13333)改为暴露PostgreSQL(5432→15432)
- 后端部署到服务器 /opt/sbrain-server, pm2管理, 端口9333
- Nginx /api/ 从 127.0.0.1:13333 改为 127.0.0.1:9333
- 服务器 .env: DB_PORT=15432 连接frp隧道
- run.md 全面更新为新架构说明
- 新增 docs/20260802-SaaS+本地数据库.md 架构方案文档
- 移除本机后端 launchd 服务(后端已上服务器)
2026-08-02 18:12:15 +08:00

134 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
当前数据库是本机Homebrew PostgreSQL 15,数据库名为 bill_query,端口5432,仅监听 localhost;当前用户 freedak 是超级用户,本机认证方式为 trust。这适合本机开发,但不适合直接照搬到服务器生产环境。前端浏览器也不应直接连接PostgreSQL,必须通过后端API访问。
## 登录账号
| 用户名 | 密码 | 角色 |
|--------|------|------|
| 总部管理员 | 123 | hq |
| 区域经理 | 123 | regional |
| 潘家园店长 | 123 | store |
| 商品部 | 123 | dept |
## 架构(2026-08-02 更新:SaaS + 本地数据库)
```
浏览器 → https://dm.all8ai.top
→ Nginx(服务器) → /api/ → 127.0.0.1:9333 (服务器后端, pm2管理)
→ frp隧道 127.0.0.1:15432 → 本机PostgreSQL:5432
```
- **前端**:服务器 Nginx 静态文件 `/var/www/dm.all8ai.top`
- **后端**:服务器 `/opt/sbrain-server`pm2 管理,端口 9333
- **数据库**:本机 PostgreSQL 15,端口 5432,数据库 bill_query
- **frp 隧道**:暴露 PostgreSQL5432→15432),服务器后端通过 15432 连接本机数据库
## 部署流程
### 前端部署
```bash
bash deploy.sh
```
构建前端 + scp 上传到服务器 `/var/www/dm.all8ai.top`
### 后端部署
```bash
bash deploy-server.sh
```
rsync 同步代码到服务器 `/opt/sbrain-server` + npm install + pm2 重启。
### 本机 frp 隧道(launchd 系统级服务)
frpc 配置(`/Users/freedak/frp/frpc.toml`):
```toml
serverAddr = "152.136.182.184"
serverPort = 7000
loginFailExit = false
auth.token = "frp_gitea_2026"
[[proxies]]
name = "dm-db"
type = "tcp"
localIP = "127.0.0.1"
localPort = 5432
remotePort = 15432
```
已配置为 macOS launchd 系统级服务,开机自动启动,崩溃自动重启,与 IDE 无关。
| 服务 | plist | 日志 |
|------|-------|------|
| frp 隧道 (5432→15432) | `~/Library/LaunchAgents/com.frp.client.plist` | `~/Library/Logs/frpc.log` |
#### 管理命令
```bash
# 停止 frpc
launchctl bootout gui/$(id -u)/com.frp.client
# 启动 frpc
launchctl load ~/Library/LaunchAgents/com.frp.client.plist
# 查看日志
tail -f ~/Library/Logs/frpc.log
# 检查状态
pgrep -l frpc
curl -s https://dm.all8ai.top/api/health
```
#### 手动启动 frpc(备用,launchd 未运行时)
```bash
cd /Users/freedak/frp && ./frpc -c frpc.toml &
```
## 本地开发
### 后端开发
```bash
cd server && npm run dev
```
后端运行在 `http://localhost:3333`,连接本地 PostgreSQL`localhost:5432`,数据库 `bill_query`)。
### 前端开发
```bash
cd client && npm run dev
```
访问 `http://localhost:5173`Vite proxy 自动转发 `/api``localhost:3333`)。
## 服务器后端管理
```bash
# 查看后端状态
ssh ubuntu@152.136.182.184 'pm2 status sbrain-server'
# 查看后端日志
ssh ubuntu@152.136.182.184 'pm2 logs sbrain-server --lines 50'
# 重启后端
ssh ubuntu@152.136.182.184 'pm2 restart sbrain-server'
# 查看后端 .env
ssh ubuntu@152.136.182.184 'cat /opt/sbrain-server/.env'
```
## 注意事项
- 本机 PostgreSQL 端口 5432 被 Homebrew PostgreSQL 占用,数据库 `bill_query` 在本地
- frp 隧道将远程 15432 映射到本地 5432,服务器后端通过 15432 连接本机数据库
- 本机只需运行 frpc(launchd 自启),后端已部署到服务器
- 修改后端代码后执行 `bash deploy-server.sh` 部署到服务器
- 修改前端代码后执行 `bash deploy.sh` 部署到服务器
- `server/.env`(本机开发用)中 `DB_PORT=5432` 连接本地数据库,不要改为 5433
- 服务器 `/opt/sbrain-server/.env``DB_PORT=15432` 连接 frp 隧道端口
- frpc 断开后线上前端 API 会失效,需重新启动 frpc
- launchd 服务开机自启,关闭 IDE/终端不影响;仅注销/关机时停止,下次登录/开机自动恢复