-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
338 lines (298 loc) · 13.3 KB
/
Makefile
File metadata and controls
338 lines (298 loc) · 13.3 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
.PHONY: proto clean-proto gen-auth gen-commercial gen-features gen-levels gen-dynasty gen-support gen-training gen-notifications gen-calendar gen-storage gen-financial gen-all help build-all deploy-all test up down restart logs ps build clean dev dev-up dev-down test-coverage-financial test-coverage-social
# Proto generation
PROTO_DIR=shared/proto
PROTO_OUT_DIR=shared/pb
# Docker
DOCKER_REGISTRY=metargb
VERSION?=latest
# Docker Compose compatibility - auto-detect docker-compose or docker compose plugin
# Windows PowerShell doesn't support 'command -v', so default to 'docker compose' (modern Docker Desktop)
ifeq ($(OS),Windows_NT)
DOCKER_COMPOSE := docker compose
else
DOCKER_COMPOSE := $(shell command -v docker-compose 2> /dev/null || echo "docker compose")
endif
help:
@echo "Available targets:"
@echo ""
@echo "Proto Generation:"
@echo " proto - Generate all proto files"
@echo " gen-auth - Generate auth service proto"
@echo " gen-commercial - Generate commercial service proto"
@echo " gen-features - Generate features service proto"
@echo " gen-levels - Generate levels service proto"
@echo " clean-proto - Clean generated proto files"
@echo ""
@echo "Build:"
@echo " build-all - Build all service Docker images"
@echo " build-features - Build features service Docker image"
@echo " build-levels - Build levels service Docker image"
@echo ""
@echo "Deploy:"
@echo " deploy-all - Deploy all services to Kubernetes"
@echo ""
@echo "Test:"
@echo " test - Run integration tests"
@echo " test-all - Run all test suites"
@echo " test-coverage-features - features-service handler coverage ≥70% (GOWORK=off)"
@echo " test-coverage-financial - financial-service handler coverage ≥70%"
@echo " test-coverage-social - social-service handler+service coverage ≥70%"
@echo ""
@echo "Database:"
@echo " import-schema - Import database schema only (schema.sql)"
@echo " import-database - Import database with data (metargb_db.sql)"
@echo ""
@echo "Docker:"
@echo " up, down, build, logs, ps - Compose lifecycle"
@echo " dev-up, dev-down - Development with watch mode"
# =============================================================================
# Testing
# =============================================================================
# Unit tests
test-unit:
@echo "🧪 Running unit tests for all services..."
@for service in services/*/; do \
if [ -f "$$service/go.mod" ]; then \
echo "Testing $$(basename $$service)..."; \
cd $$service && \
if [ -d internal ]; then \
go test ./internal/... -v -race -coverprofile=coverage.out || exit 1; \
else \
go test ./... -v -race -coverprofile=coverage.out || exit 1; \
fi; \
cd ../..; \
fi \
done
@echo "✅ All unit tests passed"
# Features-service handler coverage gate (≥70%, no MySQL required; uses GOWORK=off for local replace)
test-coverage-features:
@echo "🧪 features-service handler coverage (min 70%)..."
cd services/features-service && GOWORK=off go test ./internal/handler/... -race -coverprofile=coverage.out -covermode=atomic
@pct=$$(cd services/features-service && GOWORK=off go tool cover -func=coverage.out | tail -1 | grep -oE '[0-9]+\.[0-9]+' | tail -1); \
echo "handler statements coverage: $${pct}%"; \
awk -v p="$$pct" 'BEGIN{if (p+0 < 70.0) exit 1}'
@echo "✅ features-service handler coverage OK"
# Financial-service handler coverage gate (≥70%)
test-coverage-financial:
@echo "🧪 financial-service handler coverage (min 70%)..."
cd services/financial-service && go test ./internal/handler/... -race -coverprofile=coverage.out -covermode=atomic
@pct=$$(cd services/financial-service && go tool cover -func=coverage.out | tail -1 | grep -oE '[0-9]+\.[0-9]+' | tail -1); \
echo "financial-service handler statements coverage: $${pct}%"; \
awk -v p="$$pct" 'BEGIN{if (p+0 < 70.0) exit 1}'
@echo "✅ financial-service handler coverage OK"
# Social-service handler + service coverage gate (≥70%, combined packages)
test-coverage-social:
@echo "🧪 social-service handler+service coverage (min 70%)..."
cd services/social-service && go test ./internal/handler/... ./internal/service/... -race -coverprofile=coverage.out -covermode=atomic
@pct=$$(cd services/social-service && go tool cover -func=coverage.out | tail -1 | grep -oE '[0-9]+\.[0-9]+' | tail -1); \
echo "social-service handler+service statements coverage: $${pct}%"; \
awk -v p="$$pct" 'BEGIN{if (p+0 < 70.0) exit 1}'
@echo "✅ social-service coverage OK"
# Integration tests
test-integration:
@echo "🧪 Running integration tests..."
cd tests/integration && go test -v ./...
# Golden JSON tests
test-golden:
@echo "🧪 Running golden JSON comparison tests..."
cd tests/golden && go test -v ./...
# Database tests
test-database:
@echo "🧪 Running database schema and concurrency tests..."
cd tests/database && go test -v ./...
# Run all tests
test-all: test-unit test-integration test-golden test-database
@echo "✅ All test suites passed"
# Legacy test target (kept for backward compatibility)
test: test-integration
# =============================================================================
# Docker Compose Management
# =============================================================================
.PHONY: up down restart logs ps build clean import-schema import-database help-docker dev-up dev-down dev-build dev-logs dev-restart dev-ps
up:
@echo "🚀 Starting all microservices..."
$(DOCKER_COMPOSE) up -d
@echo "✅ All services started!"
@echo ""
@echo "Services available at:"
@echo " Kong API Gateway: http://localhost:8000"
@echo " Kong Admin: http://localhost:8001"
@echo " WebSocket: http://localhost:3000"
@echo ""
@echo "Run 'make ps' to check service status"
@echo "Run 'make logs' to view logs"
down:
@echo "🛑 Stopping all microservices..."
$(DOCKER_COMPOSE) down
@echo "✅ All services stopped"
restart:
@echo "🔄 Restarting all microservices..."
$(DOCKER_COMPOSE) restart
@echo "✅ All services restarted"
logs:
$(DOCKER_COMPOSE) logs -f
ps:
@echo "📊 Service Status:"
@echo ""
$(DOCKER_COMPOSE) ps
@echo ""
@echo "Healthy services:"
@docker ps --filter "health=healthy" --format " ✅ {{.Names}}"
@echo ""
@echo "Unhealthy services:"
@docker ps --filter "health=unhealthy" --format " ❌ {{.Names}}"
build:
@echo "🔨 Building all services..."
$(DOCKER_COMPOSE) build
@echo "✅ Build complete"
build-service:
@if [ -z "$(SERVICE)" ]; then \
echo "❌ Please specify SERVICE=service-name"; \
echo "Example: make build-service SERVICE=auth-service"; \
exit 1; \
fi
@echo "🔨 Building $(SERVICE)..."
$(DOCKER_COMPOSE) build $(SERVICE)
@echo "✅ $(SERVICE) built successfully"
clean:
@echo "🧹 Cleaning up Docker resources..."
$(DOCKER_COMPOSE) down -v
docker system prune -f
@echo "✅ Cleanup complete"
import-schema:
@echo "📥 Importing database schema..."
@if [ ! -f scripts/schema.sql ]; then \
echo "❌ scripts/schema.sql not found!"; \
exit 1; \
fi
docker exec -i metargb-mysql mysql -uroot -proot_password metargb_db < scripts/schema.sql
@echo "✅ Schema imported successfully"
@echo ""
@echo "Verifying tables..."
@docker exec metargb-mysql mysql -uroot -proot_password metargb_db -e "SELECT COUNT(*) as table_count FROM information_schema.tables WHERE table_schema='metargb_db';" 2>/dev/null | grep -v table_count || echo "Could not verify"
import-database:
@echo "Importing database (schema + data) from metargb_db.sql..."
@echo "Dropping and recreating database..."
ifeq ($(OS),Windows_NT)
@docker exec -i metargb-mysql mysql -uroot -proot_password -e "DROP DATABASE IF EXISTS metargb_db; CREATE DATABASE metargb_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" 2>nul
@echo "Importing data..."
@powershell -Command "Get-Content scripts\metargb_db.sql | docker exec -i metargb-mysql mysql -uroot -proot_password metargb_db"
else
@docker exec -i metargb-mysql mysql -uroot -proot_password -e "DROP DATABASE IF EXISTS metargb_db; CREATE DATABASE metargb_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" 2>/dev/null || true
@echo "Importing data..."
@docker exec -i metargb-mysql mysql -uroot -proot_password metargb_db < scripts/metargb_db.sql
endif
@echo "Database imported successfully"
@echo ""
@echo "Verifying import..."
ifeq ($(OS),Windows_NT)
@docker exec metargb-mysql mysql -uroot -proot_password metargb_db -e "SELECT COUNT(*) as table_count FROM information_schema.tables WHERE table_schema='metargb_db';" 2>nul | findstr /v table_count || echo "Could not verify table count"
@docker exec metargb-mysql mysql -uroot -proot_password metargb_db -e "SELECT COUNT(*) as row_count FROM account_securities;" 2>nul | findstr /v row_count || echo "Could not verify data"
else
@docker exec metargb-mysql mysql -uroot -proot_password metargb_db -e "SELECT COUNT(*) as table_count FROM information_schema.tables WHERE table_schema='metargb_db';" 2>/dev/null | grep -v table_count || echo "Could not verify table count"
@docker exec metargb-mysql mysql -uroot -proot_password metargb_db -e "SELECT COUNT(*) as row_count FROM account_securities;" 2>/dev/null | grep -v row_count || echo "Could not verify data"
endif
dev:
@echo "🚀 Starting development environment..."
@echo "ℹ️ Each service uses its own config.env (copy from config.env.sample)"
@echo "Starting MySQL and Redis..."
$(DOCKER_COMPOSE) up -d mysql redis
@echo "Waiting for database to be ready..."
@sleep 10
@echo "Checking if schema needs to be imported..."
@TABLE_COUNT=$$(docker exec metargb-mysql mysql -uroot -proot_password metargb_db -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='metargb_db';" 2>/dev/null | tail -1); \
if [ "$$TABLE_COUNT" = "0" ]; then \
echo "Importing schema..."; \
make import-schema; \
else \
echo "✅ Database already initialized ($$TABLE_COUNT tables)"; \
fi
@echo ""
@echo "Starting all services..."
$(DOCKER_COMPOSE) up -d
@echo ""
@echo "✅ Development environment ready!"
@make ps
stop-service:
@if [ -z "$(SERVICE)" ]; then \
echo "❌ Please specify SERVICE=service-name"; \
exit 1; \
fi
$(DOCKER_COMPOSE) stop $(SERVICE)
start-service:
@if [ -z "$(SERVICE)" ]; then \
echo "❌ Please specify SERVICE=service-name"; \
exit 1; \
fif
$(DOCKER_COMPOSE) start $(SERVICE)
logs-service:
@if [ -z "$(SERVICE)" ]; then \
echo "❌ Please specify SERVICE=service-name"; \
exit 1; \
fi
$(DOCKER_COMPOSE) logs -f $(SERVICE)
# =============================================================================
# Development with Hot Reloading
# =============================================================================
dev-up:
@echo "🚀 Starting development environment with Docker Compose Watch..."
@echo "ℹ️ File changes will automatically trigger rebuilds (Go) or restarts (Node.js)"
@echo ""
$(DOCKER_COMPOSE) up --watch
@echo "✅ Development services started with watch mode!"
dev-down:
@echo "🛑 Stopping development services..."
$(DOCKER_COMPOSE) down
@echo "✅ Development services stopped"
dev-build:
@echo "🔨 Building development images..."
$(DOCKER_COMPOSE) build
@echo "✅ Development images built successfully"
dev-logs:
@echo "📝 Following development service logs (Ctrl+C to stop)..."
$(DOCKER_COMPOSE) logs -f
dev-restart:
@echo "🔄 Restarting development services..."
$(DOCKER_COMPOSE) restart
@echo "✅ Development services restarted"
dev-ps:
@echo "📊 Development Service Status:"
@echo ""
$(DOCKER_COMPOSE) ps
@echo ""
@echo "Healthy services:"
@docker ps --filter "health=healthy" --format " ✅ {{.Names}}"
@echo ""
@echo "Unhealthy services:"
@docker ps --filter "health=unhealthy" --format " ❌ {{.Names}}"
help-docker:
@echo "Docker Compose Commands:"
@echo ""
@echo " make dev - Start complete development environment"
@echo " make up - Start all services"
@echo " make down - Stop all services"
@echo " make restart - Restart all services"
@echo " make ps - Show service status"
@echo " make logs - Follow all service logs"
@echo " make build - Build all services"
@echo " make clean - Stop services and remove volumes"
@echo " make import-schema - Import database schema only"
@echo " make import-database - Import database with data (metargb_db.sql)"
@echo ""
@echo "Development (Docker Compose Watch):"
@echo " make dev-up - Start services with watch mode (auto-rebuild/restart)"
@echo " make dev-down - Stop development services"
@echo " make dev-build - Build development images"
@echo " make dev-logs - View logs from dev services"
@echo ""
@echo "Service-specific commands:"
@echo " make build-service SERVICE=auth-service - Build specific service"
@echo " make start-service SERVICE=auth-service - Start specific service"
@echo " make stop-service SERVICE=auth-service - Stop specific service"
@echo " make logs-service SERVICE=auth-service - View service logs"
@echo ""
@echo "Examples:"
@echo " make dev - Complete setup"
@echo " make dev-up - Start with watch mode (auto-rebuild/restart)"
@echo " make logs-service SERVICE=auth-service - View auth logs"
@echo " make restart - Restart everything"