refactor: reorganize project structure and improve code quality

- Move CI/CD configs to config/ci/ directory
- Reorganize scripts into categorized directories (deployment, monitoring, testing, utils)
- Consolidate documentation into docs/ directory with proper structure
- Update linting and testing configurations
- Remove obsolete test reports and performance summaries
- Add new documentation for code quality tools and contact form security
- Improve project organization and maintainability
- Fix lint-staged config to only lint JS/TS files
- Disable react/react-in-jsx-scope rule for Next.js compatibility
- Ignore scripts and test config directories in ESLint
This commit is contained in:
张翔
2026-03-24 13:38:58 +08:00
parent c06ac08510
commit 498bb3a3c8
62 changed files with 5473 additions and 6498 deletions
@@ -0,0 +1,44 @@
#!/bin/bash
echo "🔧 监控和告警系统环境检查..."
# 检查 Docker
if ! command -v docker &> /dev/null; then
echo "❌ Docker 未安装"
echo "请访问 https://docs.docker.com/get-docker/ 安装 Docker"
exit 1
else
echo "✅ Docker 已安装: $(docker --version)"
fi
# 检查 Docker Compose
if ! command -v docker-compose &> /dev/null; then
echo "❌ Docker Compose 未安装"
echo "请运行: sudo curl -L \"https://github.com/docker/compose/releases/latest/download/docker-compose-\$(uname -s)-\$(uname -m)\" -o /usr/local/bin/docker-compose"
echo "然后运行: sudo chmod +x /usr/local/bin/docker-compose"
exit 1
else
echo "✅ Docker Compose 已安装: $(docker-compose --version)"
fi
# 检查端口占用
echo ""
echo "📊 检查端口占用情况..."
check_port() {
if lsof -Pi :$1 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "⚠️ 端口 $1 已被占用"
return 1
else
echo "✅ 端口 $1 可用"
return 0
fi
}
check_port 3000 # 应用服务
check_port 9090 # Prometheus
check_port 3001 # Grafana
check_port 9093 # Alertmanager
echo ""
echo "✅ 环境检查完成!"
@@ -0,0 +1,199 @@
#!/bin/bash
set -e
echo "🚀 轻量级监控系统配置"
echo "========================"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 检查 Sentry 配置
check_sentry() {
echo -e "${YELLOW}📋 检查 Sentry 配置...${NC}"
if [ -z "$NEXT_PUBLIC_SENTRY_DSN" ]; then
echo -e "${RED}❌ Sentry DSN 未配置${NC}"
echo "请在 .env.production 中设置 NEXT_PUBLIC_SENTRY_DSN"
return 1
fi
echo -e "${GREEN}✅ Sentry 已配置${NC}"
}
# 配置 Google Analytics
setup_google_analytics() {
echo -e "${YELLOW}📋 配置 Google Analytics...${NC}"
echo "请输入 Google Analytics 测量 ID"
read -p "GA Measurement ID (格式: G-XXXXXXXXXX): " ga_id
if [ -z "$ga_id" ]; then
echo -e "${YELLOW}⚠️ 跳过 Google Analytics 配置${NC}"
return 0
fi
# 更新环境变量
if [ -f ".env.production" ]; then
if grep -q "NEXT_PUBLIC_GA_MEASUREMENT_ID" .env.production; then
sed -i.bak "s/NEXT_PUBLIC_GA_MEASUREMENT_ID=.*/NEXT_PUBLIC_GA_MEASUREMENT_ID=$ga_id/" .env.production
else
echo "NEXT_PUBLIC_GA_MEASUREMENT_ID=$ga_id" >> .env.production
fi
echo -e "${GREEN}✅ Google Analytics 已配置${NC}"
else
echo -e "${RED}❌ 找不到 .env.production 文件${NC}"
return 1
fi
}
# 配置 UptimeRobot
setup_uptime_robot() {
echo -e "${YELLOW}📋 配置 UptimeRobot...${NC}"
echo ""
echo "请按照以下步骤配置 UptimeRobot"
echo ""
echo "1. 访问 https://uptimerobot.com/"
echo "2. 注册免费账号"
echo "3. 创建新的 Monitor"
echo " - Monitor Type: HTTP(s)"
echo " - URL: https://www.novalon.cn"
echo " - Monitoring Interval: 5 minutes"
echo "4. 添加 Alert Contacts:"
echo " - Email: ops@novalon.cn"
echo ""
read -p "配置完成后按 Enter 继续..."
}
# 创建健康检查端点
create_health_check() {
echo -e "${YELLOW}📋 创建健康检查端点...${NC}"
# 检查是否已存在
if [ -d "src/app/api/health" ]; then
echo -e "${GREEN}✅ 健康检查端点已存在${NC}"
return 0
fi
mkdir -p src/app/api/health
cat > src/app/api/health/route.ts << 'EOF'
import { NextResponse } from 'next/server';
export async function GET() {
try {
// 检查数据库连接
const dbConnected = await checkDatabaseConnection();
// 获取系统信息
const uptime = process.uptime();
return NextResponse.json({
status: 'healthy',
timestamp: new Date().toISOString(),
version: process.env.npm_package_version || '1.0.0',
checks: {
database: dbConnected ? 'connected' : 'disconnected',
uptime: Math.floor(uptime)
}
});
} catch (error) {
return NextResponse.json({
status: 'unhealthy',
timestamp: new Date().toISOString(),
error: 'Health check failed'
}, { status: 500 });
}
}
async function checkDatabaseConnection(): Promise<boolean> {
try {
// 这里添加实际的数据库连接检查逻辑
return true;
} catch {
return false;
}
}
EOF
echo -e "${GREEN}✅ 健康检查端点已创建${NC}"
}
# 配置 Sentry 告警
setup_sentry_alerts() {
echo -e "${YELLOW}📋 配置 Sentry 告警...${NC}"
echo ""
echo "请按照以下步骤配置 Sentry 告警:"
echo ""
echo "1. 登录 Sentry Dashboard"
echo "2. 进入 Settings → Alerts"
echo "3. 创建新的 Alert Rule"
echo " - Issue: Critical Errors"
echo " - Environment: Production"
echo " - Frequency: Immediately"
echo " - Email: ops@novalon.cn"
echo ""
read -p "配置完成后按 Enter 继续..."
}
# 显示配置摘要
show_summary() {
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}🎉 轻量级监控配置完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "📊 监控组件:"
echo ""
echo -e " • Sentry: ${GREEN}${NC} 错误监控和性能追踪"
echo -e " • UptimeRobot: ${YELLOW}⚠️ 需要手动配置${NC} 可用性监控"
echo -e " • Google Analytics: ${YELLOW}⚠️ 需要手动配置${NC} 访问统计"
echo -e " • Health Check: ${GREEN}${NC} 内部服务状态"
echo -e " • Email Alerts: ${GREEN}${NC} ops@novalon.cn"
echo ""
echo "📋 下一步操作:"
echo ""
echo " 1. 配置 UptimeRobot: https://uptimerobot.com/"
echo " 2. 配置 Google Analytics: https://analytics.google.com/"
echo " 3. 配置 Sentry 告警: Sentry Dashboard → Settings → Alerts"
echo ""
echo "📚 详细文档: docs/LIGHTWEIGHT_MONITORING.md"
echo ""
}
# 主函数
main() {
echo ""
# 检查 Sentry
check_sentry
# 配置 Google Analytics
echo ""
read -p "是否配置 Google Analytics? (y/n): " setup_ga
if [[ $setup_ga =~ ^[Yy]$ ]]; then
setup_google_analytics
fi
# 配置 UptimeRobot
echo ""
read -p "是否配置 UptimeRobot? (y/n): " setup_uptime
if [[ $setup_uptime =~ ^[Yy]$ ]]; then
setup_uptime_robot
fi
# 创建健康检查
create_health_check
# 配置 Sentry 告警
setup_sentry_alerts
# 显示摘要
show_summary
}
# 运行主函数
main
+94
View File
@@ -0,0 +1,94 @@
#!/bin/bash
set -e
echo "🔍 配置监控和告警..."
# 创建监控配置目录
mkdir -p monitoring
# 创建Prometheus配置
cat > monitoring/prometheus.yml << 'EOF'
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'novalon-website'
static_configs:
- targets: ['localhost:3000']
metrics_path: '/api/health'
EOF
# 创建Grafana仪表板配置
cat > monitoring/grafana-dashboard.json << 'EOF'
{
"dashboard": {
"title": "Novalon Website Monitoring",
"panels": [
{
"title": "HTTP Requests",
"targets": [
{
"expr": "rate(http_requests_total[5m])"
}
]
},
{
"title": "Response Time",
"targets": [
{
"expr": "histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))"
}
]
},
{
"title": "Error Rate",
"targets": [
{
"expr": "rate(http_requests_total{status=~\"5..\"}[5m])"
}
]
}
]
}
}
EOF
# 创建告警规则
cat > monitoring/alerts.yml << 'EOF'
groups:
- name: novalon-website
rules:
- alert: HighErrorRate
expr: rate(http_requests_total{status=~\"5..\"}[5m]) > 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "High error rate detected"
description: "Error rate is {{ $value }}"
- alert: HighResponseTime
expr: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) > 1
for: 5m
labels:
severity: warning
annotations:
summary: "High response time detected"
description: "95th percentile response time is {{ $value }}s"
- alert: ServiceDown
expr: up{job=\"novalon-website\"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Service is down"
description: "Novalon website service is not responding"
EOF
echo "✅ 监控和告警配置完成!"
echo "📊 Prometheus配置: monitoring/prometheus.yml"
echo "📈 Grafana仪表板: monitoring/grafana-dashboard.json"
echo "🚨 告警规则: monitoring/alerts.yml"
+200
View File
@@ -0,0 +1,200 @@
#!/bin/bash
set -e
echo "🚀 监控和告警系统快速启动脚本"
echo "=================================="
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 检查 Docker
check_docker() {
echo -e "${YELLOW}📋 检查 Docker 环境...${NC}"
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker 未安装${NC}"
echo "请访问 https://docs.docker.com/get-docker/ 安装 Docker"
exit 1
fi
echo -e "${GREEN}✅ Docker 已安装${NC}"
}
# 检查 Docker Compose
check_docker_compose() {
echo -e "${YELLOW}📋 检查 Docker Compose...${NC}"
if ! command -v docker-compose &> /dev/null; then
echo -e "${RED}❌ Docker Compose 未安装${NC}"
echo "请运行: sudo apt-get install docker-compose"
exit 1
fi
echo -e "${GREEN}✅ Docker Compose 已安装${NC}"
}
# 检查端口占用
check_ports() {
echo -e "${YELLOW}📋 检查端口占用...${NC}"
ports=(3000 9090 3001 9093)
port_names=("应用服务" "Prometheus" "Grafana" "Alertmanager")
for i in "${!ports[@]}"; do
port=${ports[$i]}
name=${port_names[$i]}
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
echo -e "${YELLOW}⚠️ 端口 $port ($name) 已被占用${NC}"
else
echo -e "${GREEN}✅ 端口 $port ($name) 可用${NC}"
fi
done
}
# 创建必要的目录
create_directories() {
echo -e "${YELLOW}📋 创建必要的目录...${NC}"
mkdir -p monitoring
mkdir -p logs
mkdir -p data
echo -e "${GREEN}✅ 目录创建完成${NC}"
}
# 配置邮件服务
configure_email() {
echo -e "${YELLOW}📋 配置邮件服务...${NC}"
if [ ! -f "monitoring/alertmanager.yml" ]; then
echo -e "${RED}❌ 找不到 alertmanager.yml 配置文件${NC}"
exit 1
fi
echo "请输入邮件配置信息:"
read -p "SMTP 服务器 (默认: smtp.resend.com): " smtp_server
smtp_server=${smtp_server:-smtp.resend.com}
read -p "SMTP 端口 (默认: 587): " smtp_port
smtp_port=${smtp_port:-587}
read -p "SMTP 用户名 (默认: resend): " smtp_username
smtp_username=${smtp_username:-resend}
read -p "SMTP 密码 (Resend API Key): " smtp_password
read -p "发件人邮箱 (默认: alertmanager@novalon.cn): " from_email
from_email=${from_email:-alertmanager@novalon.cn}
read -p "接收告警的邮箱 (多个邮箱用逗号分隔): " to_emails
# 更新配置文件
sed -i.bak "s|smarthost: '.*'|smarthost: '$smtp_server:$smtp_port'|g" monitoring/alertmanager.yml
sed -i.bak "s|auth_username: '.*'|auth_username: '$smtp_username'|g" monitoring/alertmanager.yml
sed -i.bak "s|auth_password: '.*'|auth_password: '$smtp_password'|g" monitoring/alertmanager.yml
sed -i.bak "s|from: '.*'|from: '$from_email'|g" monitoring/alertmanager.yml
sed -i.bak "s|to: '.*'|to: '$to_emails'|g" monitoring/alertmanager.yml
echo -e "${GREEN}✅ 邮件配置完成${NC}"
}
# 启动监控服务
start_monitoring() {
echo -e "${YELLOW}📋 启动监控服务...${NC}"
if [ ! -f "docker-compose.monitoring.yml" ]; then
echo -e "${RED}❌ 找不到 docker-compose.monitoring.yml 文件${NC}"
exit 1
fi
docker-compose -f docker-compose.monitoring.yml up -d
echo -e "${GREEN}✅ 监控服务启动完成${NC}"
}
# 等待服务启动
wait_for_services() {
echo -e "${YELLOW}📋 等待服务启动...${NC}"
echo "等待 Prometheus 启动..."
for i in {1..30}; do
if curl -s http://localhost:9090/-/healthy > /dev/null 2>&1; then
echo -e "${GREEN}✅ Prometheus 已启动${NC}"
break
fi
sleep 2
done
echo "等待 Grafana 启动..."
for i in {1..30}; do
if curl -s http://localhost:3001/api/health > /dev/null 2>&1; then
echo -e "${GREEN}✅ Grafana 已启动${NC}"
break
fi
sleep 2
done
echo "等待 Alertmanager 启动..."
for i in {1..30}; do
if curl -s http://localhost:9093/-/healthy > /dev/null 2>&1; then
echo -e "${GREEN}✅ Alertmanager 已启动${NC}"
break
fi
sleep 2
done
}
# 显示访问信息
show_access_info() {
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}🎉 监控和告警系统启动成功!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "📊 监控服务访问地址:"
echo ""
echo -e " • Prometheus: ${YELLOW}http://localhost:9090${NC}"
echo -e " • Grafana: ${YELLOW}http://localhost:3001${NC} (admin/admin)"
echo -e " • Alertmanager: ${YELLOW}http://localhost:9093${NC}"
echo ""
echo "📋 下一步操作:"
echo ""
echo " 1. 访问 Grafana (http://localhost:3001)"
echo " 2. 登录 (用户名: admin, 密码: admin)"
echo " 3. 添加 Prometheus 数据源"
echo " 4. 导入监控仪表板"
echo ""
echo "📚 详细配置文档: docs/MONITORING_SETUP.md"
echo ""
}
# 主函数
main() {
echo ""
# 检查环境
check_docker
check_docker_compose
check_ports
# 创建目录
create_directories
# 询问是否配置邮件
echo ""
read -p "是否配置邮件服务? (y/n): " configure_email_choice
if [[ $configure_email_choice =~ ^[Yy]$ ]]; then
configure_email
fi
# 启动服务
start_monitoring
# 等待服务启动
wait_for_services
# 显示访问信息
show_access_info
}
# 运行主函数
main