douyinWEB/AGENTS.md
eric 9d87aa48be AGENTS: server data is the single source of truth
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-11 04:27:21 -05:00

130 lines
3.9 KiB
Markdown
Raw Permalink 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.

# douyinWEB — Cursor AI 指导文档
## 项目概述
抖音直播间监控 + YouTube Shorts 上传分发系统。
- **后端**:FastAPI(`backend/app/`)+ SQLite(`backend/data/monitor.db`)
- **前端**:Next.js 15(`frontend/src/`)
- **桌面端**:Electron + React(`desktop/src/`)
- **线上地址**:https://dylive.nomadro.com
- **服务器**:`107.173.30.245`(root,Ubuntu,`/opt/douyinWEB`)
---
## Git 管理规范(强制要求)
### 分支
| 分支 | 说明 |
|------|------|
| `main` | Windows 本机快照,不部署到服务器 |
| `dev` | 服务器线上运行代码,所有开发在此分支 |
### 每次代码变更必须走完整流程
```
1. 本地修改代码(dev 分支)
2. git add <changed files>
3. git commit -m "描述"
4. git push origin dev
5. 服务器: git pull origin dev
6. systemctl restart douyinweb-api
7. cd /opt/douyinWEB/frontend && npm run build
8. systemctl restart douyinweb-web
```
**git push 后,Windows 本机和服务器都必须立即更新。**
### 禁止行为
- **禁止 sftp/scp 直接覆盖服务器代码文件**(必须走 git)
- **禁止在服务器上直接编辑代码**(vi/nano 等)
- **禁止** `git reset --hard`、`git stash apply` 等破坏服务器工作区的操作
- **禁止将运行时数据文件纳入 commit**:`monitor.db`、`pb_data/*.db`、`*.db-shm`、`*.db-wal`、`covers/`、`clips/`
### 快速部署
```bash
python scripts/_deploy.py
```
该脚本执行:服务器 `git pull origin dev` → restart API → `npm run build` → restart web。
---
## 开发约定
### 后端(Python / FastAPI)
- 数据库操作统一在 `backend/app/db.py`,加锁用 `with _lock:`
- 新增 API 路由放 `backend/app/main.py`,遵循已有命名风格
- 平台过滤用 `_platform_clause(platform, room_col=..., meta_col=...)`,勿自行拼 SQL
- YouTube 相关:`shorts_*.py`、`shorts_yt_quota.py`;抖音监控:`monitor.py`、`douyin.py`
- 不要在监控主路径里引入 Shorts 相关异常(隔离原则)
### 前端(Next.js / TypeScript)
- 抖音监控页面(`/`、`/slots`)接口调用必须加 `platform=douyin`,防止 YouTube 数据混入
- 时区统一北京时间,使用 `fmtTs`、`todayBeijingISO`、`beijingHourFromTs`(`@/lib/api`)
- 组件:`Shell`(导航)、`SessionCharts`、`SessionDetailModal`、`PlatformBadge`、`YouTubeLiveBadge`
- SSE 实时事件:`useEventStream`(`@/lib/realtime`)
### 服务器服务
| 服务 | systemd | 端口 |
|------|---------|------|
| FastAPI | `douyinweb-api` | 8014 |
| Next.js | `douyinweb-web` | 4015 |
| PocketBase | `douyinweb-pb` | 8092 |
| venv | `/opt/douyinWEB/backend/.venv/bin/python` | — |
### 重要路径
```
/opt/douyinWEB/ # 服务器仓库根
/opt/douyinWEB/backend/data/monitor.db # 主数据库(勿覆盖)
/opt/douyinWEB/backend/data/covers/ # 封面截图
/opt/douyinWEB/backend/data/clips/ # Shorts 成片
```
---
## 常见操作
### 部署代码变更
```python
# scripts/_deploy.py 已封装完整流程
python scripts/_deploy.py
```
### 检查服务器状态
```python
python scripts/_check_server_state.py
```
### 查看 API 日志
```python
python scripts/_api_logs.py
```
---
## 数据安全
### 核心原则:服务器数据永远是唯一真实来源
- **禁止**用任何方式将本地数据文件推送或覆盖到服务器
- `monitor.db`、`pb_data/*.db` 已从 git 追踪中移除,永远不纳入 commit
- 本地的 `monitor.db` 仅供本地开发调试,与线上数据无关
- 任何涉及数据库的操作,必须直接在服务器上执行,不得经由 git 同步
- `monitor.db` 是线上核心数据,任何操作前必须备份:
```bash
cp /opt/douyinWEB/backend/data/monitor.db /tmp/monitor.db.bak_$(date +%s)
```
- `shorts_channels` 表存储 YouTube OAuth token,泄露会导致频道失控
- 密码哈希存于 `kv_settings` 表,key = `admin_password_hash`,用 `SHA256(SESSION_SECRET:password)`