Skip to content

Commit a6b6c23

Browse files
committed
Sync production system and repository governance
1 parent 61b953d commit a6b6c23

50 files changed

Lines changed: 3463 additions & 2408 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1-
# TG 监控告警系统 - 环境配置
2-
# 复制此文件为 .env 并填入实际值
1+
# tg-monitor-v2 environment template
2+
# Copy to .env and fill real values. Never commit .env.
33

4-
# ==================== 数据库配置 ====================
5-
DATABASE_TYPE=mysql
6-
MYSQL_HOST=localhost
7-
MYSQL_PORT=3306
8-
MYSQL_USER=root
9-
MYSQL_PASSWORD=your_mysql_password
10-
MYSQL_DATABASE=tg_monitor
11-
12-
# ==================== Telegram API ====================
13-
# 访问 https://my.telegram.org/apps 获取
14-
TELEGRAM_API_ID=your_api_id
15-
TELEGRAM_API_HASH=your_api_hash
16-
17-
# ==================== 服务器配置 ====================
4+
# Server
185
HOST=0.0.0.0
196
PORT=8000
207
DEBUG=false
218
LOG_LEVEL=INFO
9+
TIMEZONE=Asia/Shanghai
2210

23-
# ==================== 安全配置 ====================
24-
# 生产环境请修改此密钥
25-
SECRET_KEY=please-change-this-in-production
26-
# Token 有效期(分钟)
11+
# MySQL
12+
MYSQL_HOST=localhost
13+
MYSQL_PORT=3306
14+
MYSQL_USER=tgmonitor
15+
MYSQL_PASSWORD=change_me
16+
MYSQL_DATABASE=tg_monitor
17+
18+
# Telegram API
19+
# Create an app at https://my.telegram.org/apps
20+
TELEGRAM_API_ID=123456
21+
TELEGRAM_API_HASH=change_me
22+
23+
# Security
24+
# Generate with: python3 -c "import secrets; print(secrets.token_urlsafe(48))"
25+
JWT_SECRET_KEY=change_me_to_a_long_random_value
2726
ACCESS_TOKEN_EXPIRE_MINUTES=240
2827

29-
# ==================== CORS 配置 ====================
30-
# 生产环境请设置具体域名,如:https://yourdomain.com
31-
# CORS_ORIGINS=https://yourdomain.com,https://app.yourdomain.com
28+
# CORS
29+
# Production example: CORS_ORIGINS=https://monitor.example.com
3230
CORS_ORIGINS=*
3331

34-
# ==================== WebSocket 令牌(可选)====================
35-
# 设置后 WebSocket 连接需要此令牌
36-
# WS_TOKEN=your_ws_token
32+
# WebSocket auth, optional
33+
# WS_TOKEN=change_me
3734

38-
# ==================== 代理配置(可选)====================
35+
# Proxy, optional
3936
# HTTP_PROXY=http://127.0.0.1:7890
4037
# HTTPS_PROXY=http://127.0.0.1:7890
4138
# SOCKS5_PROXY=socks5://127.0.0.1:7891
4239

43-
# ==================== 通知配置(可选)====================
44-
# SMTP_HOST=smtp.gmail.com
40+
# SMTP notification, optional
41+
# SMTP_HOST=smtp.example.com
4542
# SMTP_PORT=587
46-
# SMTP_USER=your_email@gmail.com
47-
# SMTP_PASSWORD=your_app_password
48-
# SMTP_FROM=your_email@gmail.com
43+
# SMTP_USER=notice@example.com
44+
# SMTP_PASSWORD=change_me
45+
# SMTP_FROM=notice@example.com
46+
47+
# Frontend, optional
48+
# Leave empty for same-origin /api/v1 access.
49+
# VITE_API_BASE_URL=http://localhost:8000

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @chu0119

.github/pull_request_template.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Summary
2+
3+
-
4+
5+
## Verification
6+
7+
- [ ] Backend syntax check or tests passed
8+
- [ ] Frontend build passed
9+
- [ ] UI screenshots attached when applicable
10+
- [ ] No `.env`, session files, logs, exports or backups included
11+
12+
## Deployment Notes
13+
14+
-

.github/workflows/ci.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build-and-check:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: "3.11"
21+
22+
- name: Backend dependencies
23+
working-directory: backend
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install -r requirements.txt
27+
28+
- name: Backend syntax check
29+
working-directory: backend
30+
run: python -m compileall app
31+
32+
- name: Setup Node
33+
uses: actions/setup-node@v4
34+
with:
35+
node-version: "20"
36+
cache: npm
37+
cache-dependency-path: frontend/package-lock.json
38+
39+
- name: Frontend dependencies
40+
working-directory: frontend
41+
run: npm ci
42+
43+
- name: Frontend build
44+
working-directory: frontend
45+
run: npm run build

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
.env
33
*.env
44
backend/.env
5+
frontend/.env
6+
!.env.example
7+
!backend/.env.example
8+
!frontend/.env.example
59

610
# Python
711
__pycache__/
@@ -10,20 +14,26 @@ __pycache__/
1014
*.egg-info/
1115
venv/
1216
.venv/
17+
.pytest_cache/
18+
.mypy_cache/
19+
.ruff_cache/
1320

1421
# Node
1522
node_modules/
1623
frontend/node_modules/
1724
dist/
1825
frontend/dist/
26+
.vite/
1927

2028
# 数据库
2129
*.db
30+
*.sqlite
2231
*.sqlite3
2332

2433
# 日志
2534
logs/
2635
*.log
36+
*.log.*
2737

2838
# Telegram sessions(敏感)
2939
sessions/
@@ -35,6 +45,7 @@ backend/sessions/
3545
backups/
3646
backend/backups/
3747
uploads/
48+
backend/uploads/
3849

3950
# 导出数据
4051
exports/
@@ -51,11 +62,14 @@ Thumbs.db
5162

5263
# 部署标记
5364
.deployed
65+
.last_restart
5466

5567
# 代理内核(太大,通过start.sh下载)
5668
backend/app/proxy/mihomo
5769

5870
# 临时文件
5971
*.pid
6072
*.tmp
73+
*.tar.gz
74+
*.zip
6175
proxy/config.yaml

CONTRIBUTING.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Contributing
2+
3+
This repository uses `main` as the stable production branch.
4+
5+
## Required Workflow
6+
7+
1. Create a feature branch from `main`.
8+
2. Commit focused changes with a clear message.
9+
3. Open a Pull Request into `main`.
10+
4. Wait for CI to pass.
11+
5. Wait for owner review before merge.
12+
13+
Direct pushes to `main` should be avoided after branch protection is enabled.
14+
15+
## Local Checks
16+
17+
Backend:
18+
19+
```bash
20+
cd backend
21+
python -m compileall app
22+
```
23+
24+
Frontend:
25+
26+
```bash
27+
cd frontend
28+
npm ci
29+
npm run build
30+
```
31+
32+
## Do Not Commit
33+
34+
- `.env` files.
35+
- Telegram `*.session` files.
36+
- Logs, backups, exports, uploads and databases.
37+
- `node_modules/`, `frontend/dist/`, Python virtual environments.
38+
39+
## Pull Request Expectations
40+
41+
- Describe what changed and why.
42+
- Mention deployment or migration steps when relevant.
43+
- Include screenshots for dashboard or big-screen UI changes.
44+
- Keep unrelated refactors out of production fixes.

DEPLOYMENT.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Deployment Guide
2+
3+
This document describes the recommended deployment, upgrade, backup and rollback flow for `tg-monitor-v2`.
4+
5+
## 1. System Requirements
6+
7+
- Linux server: Ubuntu 22.04/24.04, Debian 12, or compatible distribution.
8+
- Python 3.11 or newer.
9+
- Node.js 20 or newer.
10+
- MySQL 8.x.
11+
- A Telegram API ID and API Hash from <https://my.telegram.org/apps>.
12+
- Optional proxy for Telegram connectivity.
13+
14+
## 2. Fresh Installation
15+
16+
```bash
17+
git clone https://github.com/chu0119/tg-monitor-v2.git
18+
cd tg-monitor-v2
19+
cp .env.example .env
20+
vim .env
21+
bash install.sh
22+
```
23+
24+
After installation:
25+
26+
```bash
27+
./monitorctl.sh status
28+
./health-check.sh
29+
```
30+
31+
## 3. Manual Service Installation
32+
33+
```bash
34+
cd tg-monitor-v2
35+
bash install-services.sh
36+
sudo systemctl enable tgmonitor-backend.service
37+
sudo systemctl enable tgmonitor-frontend.service
38+
sudo systemctl start tgmonitor-backend.service
39+
sudo systemctl start tgmonitor-frontend.service
40+
```
41+
42+
## 4. Upgrade Existing Deployment
43+
44+
Before upgrading, create a backup:
45+
46+
```bash
47+
./monitorctl.sh backup
48+
```
49+
50+
Then update the code:
51+
52+
```bash
53+
git fetch origin
54+
git checkout main
55+
git pull --ff-only origin main
56+
57+
cd frontend
58+
npm ci
59+
npm run build
60+
61+
cd ../backend
62+
source venv/bin/activate
63+
pip install -r requirements.txt
64+
65+
sudo systemctl restart tgmonitor-backend.service
66+
sudo systemctl restart tgmonitor-frontend.service
67+
```
68+
69+
Verify:
70+
71+
```bash
72+
curl -f http://127.0.0.1:8000/health
73+
curl -f http://127.0.0.1:3000/
74+
```
75+
76+
## 5. Backup and Restore
77+
78+
Data that must be backed up:
79+
80+
- MySQL database.
81+
- `backend/sessions/` Telegram session files.
82+
- `.env` runtime configuration.
83+
- Optional uploads and exports if used operationally.
84+
85+
Data that should not be committed to Git:
86+
87+
- `.env`
88+
- `backend/sessions/`
89+
- `logs/`
90+
- `exports/`
91+
- `backups/`
92+
- `frontend/dist/`
93+
- `node_modules/`
94+
95+
## 6. Rollback
96+
97+
If a deployment fails:
98+
99+
```bash
100+
git log --oneline -5
101+
git checkout <known-good-commit>
102+
103+
cd frontend
104+
npm ci
105+
npm run build
106+
107+
sudo systemctl restart tgmonitor-backend.service
108+
sudo systemctl restart tgmonitor-frontend.service
109+
```
110+
111+
If data was changed, restore the database and session files from the backup created before upgrade.
112+
113+
## 7. Production Notes
114+
115+
- Set `JWT_SECRET_KEY` to a long random value.
116+
- Restrict `CORS_ORIGINS` to trusted domains in public deployments.
117+
- Protect `main` in GitHub and merge changes through Pull Requests.
118+
- Keep server `.env` and Telegram session files off the repository.
119+
- Monitor `/health` and service logs after every upgrade.

0 commit comments

Comments
 (0)