ccccccc / Makefile
cnmksjs's picture
Upload 49 files
24fd742 verified
# 聊天应用 Makefile
.PHONY: help install start stop restart logs clean dev deploy health backup restore build single test
# 默认目标
help:
@echo "聊天应用管理命令:"
@echo ""
@echo "🚀 部署命令:"
@echo " make install - 安装依赖和设置权限"
@echo " make start - 启动生产环境(多容器)"
@echo " make single - 启动单容器版本"
@echo " make dev - 启动开发环境"
@echo " make deploy - 部署到生产环境"
@echo ""
@echo "🔧 管理命令:"
@echo " make stop - 停止所有服务"
@echo " make restart - 重启所有服务"
@echo " make logs - 查看服务日志"
@echo " make health - 检查服务健康状态"
@echo " make test - 运行功能测试"
@echo ""
@echo "🐳 Docker命令:"
@echo " make build - 构建Docker镜像"
@echo " make clean - 清理Docker资源"
@echo ""
@echo "💾 数据命令:"
@echo " make backup - 备份数据库"
@echo " make restore - 恢复数据库"
@echo ""
# 安装和设置
install:
@echo "🔧 设置脚本权限..."
chmod +x *.sh
@echo "✅ 安装完成"
# 启动生产环境
start:
@echo "🚀 启动生产环境..."
./start.sh
# 启动开发环境
dev:
@echo "🚀 启动开发环境..."
./start-dev.sh
# 启动单容器版本
single:
@echo "🚀 启动单容器版本..."
@if command -v docker-compose >/dev/null 2>&1; then \
docker-compose -f docker-compose.single.yml up --build -d; \
else \
docker compose -f docker-compose.single.yml up --build -d; \
fi
@echo "✅ 单容器版本启动完成"
@echo "🌐 访问地址: http://localhost"
# 构建Docker镜像
build:
@echo "🐳 构建Docker镜像..."
@if command -v docker-compose >/dev/null 2>&1; then \
docker-compose build --no-cache; \
else \
docker compose build --no-cache; \
fi
@echo "✅ 镜像构建完成"
# 运行测试
test:
@echo "🧪 运行功能测试..."
./test.sh
# 停止服务
stop:
@echo "🛑 停止服务..."
./stop.sh
# 重启服务
restart: stop start
# 查看日志
logs:
@if command -v docker-compose >/dev/null 2>&1; then \
docker-compose logs -f; \
else \
docker compose logs -f; \
fi
# 检查健康状态
health:
@echo "🔍 检查服务状态..."
@if command -v docker-compose >/dev/null 2>&1; then \
docker-compose ps; \
else \
docker compose ps; \
fi
@echo ""
@echo "🧪 测试服务连接..."
@curl -f http://localhost:5000/api/health 2>/dev/null && echo "✅ 后端服务正常" || echo "❌ 后端服务异常"
@curl -f http://localhost:3000 2>/dev/null && echo "✅ 前端服务正常" || echo "❌ 前端服务异常"
# 清理Docker资源
clean:
@echo "🧹 清理Docker资源..."
@if command -v docker-compose >/dev/null 2>&1; then \
docker-compose down -v --rmi all --remove-orphans; \
else \
docker compose down -v --rmi all --remove-orphans; \
fi
docker system prune -f
@echo "✅ 清理完成"
# 部署到生产环境
deploy:
@echo "🚀 部署到生产环境..."
./deploy.sh
# 备份数据库
backup:
@echo "💾 备份数据库..."
@mkdir -p backups
@BACKUP_FILE="backups/chatapp-backup-$$(date +%Y%m%d-%H%M%S).gz"; \
docker exec chat-mongo mongodump --authenticationDatabase admin -u admin -p password123 --db chatapp --gzip --archive=$$BACKUP_FILE; \
echo "✅ 数据库已备份到: $$BACKUP_FILE"
# 恢复数据库
restore:
@echo "📥 恢复数据库..."
@if [ -z "$(FILE)" ]; then \
echo "❌ 请指定备份文件: make restore FILE=backups/backup-file.gz"; \
exit 1; \
fi
@if [ ! -f "$(FILE)" ]; then \
echo "❌ 备份文件不存在: $(FILE)"; \
exit 1; \
fi
docker exec chat-mongo mongorestore --authenticationDatabase admin -u admin -p password123 --db chatapp --gzip --archive=$(FILE) --drop
@echo "✅ 数据库恢复完成"
# 更新应用
update:
@echo "🔄 更新应用..."
git pull
@if command -v docker-compose >/dev/null 2>&1; then \
docker-compose pull; \
docker-compose up --build -d; \
else \
docker compose pull; \
docker compose up --build -d; \
fi
@echo "✅ 更新完成"