File size: 4,106 Bytes
24fd742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 聊天应用 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 "✅ 更新完成"