Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
486 lines (410 loc) · 16.5 KB
/
Copy pathMakefile
File metadata and controls
486 lines (410 loc) · 16.5 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# --- Variables ---
SHELL := /bin/bash
BINARY_NAME ?= rankr
BUILD_DIR ?= bin
BUF_VERSION ?= v1.56.0
DEFAULT_BRANCH ?= main
PROTOC_GEN_GO_VERSION ?= v1.34.2
PROTOC_GEN_GO_GRPC_VERSION ?= v1.5.1
DOCKER_COMPOSE ?= docker compose
INFRA_SCRIPT := ./deploy/script/start_infrastructure.sh
INFRA_SCRIPT_PROD := ./deploy/script/start_infrastructure_prod.sh
SERVICES := auth contributor leaderboardscoring leaderboardstat notification project realtime task userprofile webhook
# ====================================================================================
# General Go Commands
# ====================================================================================
.PHONY: test build clean mod-tidy lint install-linter help
# --- Agentic gate (see AGENTS.md) ---
.PHONY: check check-all
BASE ?= origin/main
check: ## Gate for a change: gofmt, vet, race tests and build on packages changed vs $(BASE)
@BASE=$(BASE) ./scripts/check.sh
check-all: ## Same checks on every package (red until the baseline issue is fixed)
@test -z "$$(gofmt -l $$(git ls-files '*.go' | grep -v '^protobuf/golang/'))" || (gofmt -l $$(git ls-files '*.go' | grep -v '^protobuf/golang/'); exit 1)
go vet ./...
go test -race -short -count=1 ./...
go build ./...
start: build
$(BUILD_DIR)/$(BINARY_NAME)
test:
go test -v ./...
build:
go build ./...
clean:
rm -rf $(BUILD_DIR)/
mod-tidy:
go mod tidy
lint:
golangci-lint run
install-linter:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# ====================================================================================
# Protobuf Commands
# ====================================================================================
.PHONY: proto-setup proto-setup-full proto-gen proto-lint proto-breaking proto-clean proto-format proto-deps proto-validate
.PHONY: install-protoc-plugins install-buf install-buf-force
.PHONY: proto-bsr-push proto-bsr-push-create proto-bsr-info proto-bsr-login proto-bsr-whoami update-buf-version
proto-setup: install-buf
@echo "Setting up Buf for Rankr project..."
@if [ ! -f "protobuf/buf.yaml" ]; then \
echo "Initializing Buf configuration..."; \
cd protobuf && buf mod init; \
fi
@echo "Updating protobuf dependencies..."
cd protobuf && buf dep update
@echo "Linting protobuf files..."
buf lint
@echo "Buf setup complete!"
proto-setup-full: proto-setup install-protoc-plugins
@echo "Generating Go code from protobuf..."
buf generate
@echo "Full Buf setup complete!"
proto-gen:
@echo "Generating protobuf code..."
buf generate
proto-lint:
@echo "Linting protobuf files..."
buf lint
proto-breaking:
@echo "Checking for breaking changes..."
@if git rev-parse --git-dir > /dev/null 2>&1; then \
echo "Git repository found, checking against $(DEFAULT_BRANCH) branch..."; \
buf breaking --against '.git#branch=$(DEFAULT_BRANCH)'; \
else \
echo "No Git repository found. Skipping breaking change check."; \
fi
proto-clean:
@echo "Cleaning generated protobuf files..."
find protobuf/golang -type f \( -name "*.pb.go" -o -name "*_grpc.pb.go" \) -print -delete || true
proto-format:
@echo "Formatting protobuf files..."
buf format -w
proto-deps:
@echo "Updating protobuf dependencies..."
cd protobuf && buf dep update
proto-validate:
@echo "Validating protobuf files..."
buf lint
@if git rev-parse --git-dir > /dev/null 2>&1; then \
echo "Git repository found, checking for breaking changes..."; \
buf breaking --against '.git#branch=$(DEFAULT_BRANCH)'; \
else \
echo "No Git repository found. Skipping breaking change check."; \
fi
# BSR (Buf Schema Registry) targets
proto-bsr-push:
@echo "Pushing protobuf module to BSR..."
cd protobuf && buf push
proto-bsr-push-create:
@echo "Pushing protobuf module to BSR (create if not exists)..."
cd protobuf && buf push --create
proto-bsr-info:
@echo "Getting BSR module info..."
cd protobuf && buf push --info
proto-bsr-login:
@echo "Logging into BSR..."
buf registry login
proto-bsr-whoami:
@echo "Checking BSR login status..."
buf registry whoami
update-buf-version:
@echo "Updating Buf version to $(BUF_VERSION)..."
sed -i 's/version: .*/version: $(BUF_VERSION)/' protobuf/buf.yaml
install-protoc-plugins:
@echo "Installing protoc plugins..."
@if ! command -v protoc-gen-go &> /dev/null; then \
echo "Installing protoc-gen-go..."; \
go install google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GEN_GO_VERSION); \
else \
echo "protoc-gen-go is already installed"; \
fi
@if ! command -v protoc-gen-go-grpc &> /dev/null; then \
echo "Installing protoc-gen-go-grpc..."; \
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@$(PROTOC_GEN_GO_GRPC_VERSION); \
else \
echo "protoc-gen-go-grpc is already installed"; \
fi
install-buf:
@echo "Installing Buf $(BUF_VERSION)..."
@if command -v buf &> /dev/null && buf --version &> /dev/null; then \
CURRENT_VERSION=$(buf --version | sed 's/^v//'); \
EXPECTED_VERSION=$(echo "$(BUF_VERSION)" | sed 's/^v//'); \
if [ "$CURRENT_VERSION" = "$EXPECTED_VERSION" ]; then \
echo "Buf $(BUF_VERSION) is already installed"; \
else \
echo "Buf version mismatch. Expected $(BUF_VERSION), found $CURRENT_VERSION. Reinstalling..."; \
$(MAKE) install-buf-force; \
fi; \
else \
echo "Buf not found, installing $(BUF_VERSION)..."; \
$(MAKE) install-buf-force; \
fi
install-buf-force:
@echo "Force installing Buf $(BUF_VERSION)..."
@if command -v curl &> /dev/null; then \
ARCHIVE="/tmp/buf-$(BUF_VERSION).tar.gz"; \
curl -sSL "https://github.com/bufbuild/buf/releases/download/v$(BUF_VERSION)/buf-$$(uname -s)-$$(uname -m).tar.gz" -o $$ARCHIVE && \
tar -xzf $$ARCHIVE -C /tmp && \
chmod +x /tmp/buf/bin/buf && \
sudo mv /tmp/buf/bin/buf /usr/local/bin/buf && \
rm -rf $$ARCHIVE /tmp/buf && \
echo "Buf $(BUF_VERSION) installed successfully"; \
else \
echo "curl not found. Please install Buf manually."; \
exit 1; \
fi
# ====================================================================================
# Infrastructure Commands
# ====================================================================================
.PHONY: infra-up infra-down infra-logs
.PHONY: infra-up-postgres infra-up-redis infra-up-nats infra-up-emqx
infra-up:
@echo "Starting shared infrastructure..."
bash $(INFRA_SCRIPT) up-all
infra-down:
@echo "Stopping shared infrastructure..."
bash $(INFRA_SCRIPT) down-all
infra-logs:
@echo "Showing logs for infrastructure stack..."
bash $(INFRA_SCRIPT) logs-all
infra-up-postgres:
@echo "Starting infrastructure PostgreSQL..."
bash $(INFRA_SCRIPT) up-postgres
infra-up-redis:
@echo "Starting infrastructure Redis..."
bash $(INFRA_SCRIPT) up-redis
infra-up-nats:
@echo "Starting infrastructure NATS..."
bash $(INFRA_SCRIPT) up-nats
infra-up-emqx:
@echo "Starting infrastructure EMQX..."
bash $(INFRA_SCRIPT) up-emqx
# ====================================================================================
# Production Infrastructure Commands
# ====================================================================================
.PHONY: infra-up-prod infra-down-prod infra-logs-prod infra-status-prod
infra-up-prod:
@echo "Starting production infrastructure..."
bash $(INFRA_SCRIPT_PROD) up-all
infra-down-prod:
@echo "Stopping production infrastructure..."
bash $(INFRA_SCRIPT_PROD) down-all
infra-logs-prod:
@echo "Showing production infrastructure logs..."
bash $(INFRA_SCRIPT_PROD) logs-all
infra-status-prod:
@echo "Showing production infrastructure status..."
bash $(INFRA_SCRIPT_PROD) status
# Complete production startup (infrastructure + services)
up-prod:
$(MAKE) infra-up-prod
@echo "Waiting for infrastructure to be ready..."
@sleep 10
@echo "Starting all production services..."
$(MAKE) services-up-prod
@echo "All production services are up!"
# Complete production shutdown
down-prod:
$(MAKE) services-down-prod
$(MAKE) infra-down-prod
@echo "All production services stopped!"
# Production status
status-prod:
@echo "Production container status:"
@docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "prod|shared-|NAMES"
# ====================================================================================
# Service Commands
# ====================================================================================
.PHONY: $(SERVICES:%=start-%-app-dev) $(SERVICES:%=start-%-app-dev-log) $(SERVICES:%=stop-%-app-dev)
define SERVICE_template
start-$(1)-app-dev:
@echo "Starting $(1) service..."
cd deploy/$(1)/development && PROJECT_ROOT=$(CURDIR) $(DOCKER_COMPOSE) up -d --build
start-$(1)-app-dev-log:
@echo "Starting $(1) service with logs..."
cd deploy/$(1)/development && PROJECT_ROOT=$(CURDIR) $(DOCKER_COMPOSE) up --build
stop-$(1)-app-dev:
@echo "Stopping $(1) service..."
cd deploy/$(1)/development && PROJECT_ROOT=$(CURDIR) $(DOCKER_COMPOSE) down
endef
$(foreach svc,$(SERVICES),$(eval $(call SERVICE_template,$(svc))))
.PHONY: services-up services-down services-logs
# All services commands
services-up:
@echo "Starting all application services..."
@for svc in $(SERVICES); do \
$(MAKE) start-$$svc-app-dev; \
done
@echo "✅ All services are up!"
services-down:
@echo "Stopping all application services..."
@for svc in $(SERVICES); do \
$(MAKE) stop-$$svc-app-dev; \
done
@echo "✅ All services stopped!"
services-logs:
@echo "Showing all services logs..."
@trap 'for job in $$(jobs -p); do kill $$job 2>/dev/null || true; done' SIGINT SIGTERM EXIT; \
for svc in $(SERVICES); do \
( \
echo ""; \
echo ">>> $$svc logs"; \
cd deploy/$$svc/development && $(DOCKER_COMPOSE) logs -f \
) & \
done; \
wait
# Complete startup (infrastructure + services)
up:
$(MAKE) infra-up
@echo "Waiting for infrastructure to be ready..."
@sleep 10
@echo "Starting all services..."
$(MAKE) services-up
@echo "✅ All services are up!"
# Complete shutdown
down:
$(MAKE) services-down
$(MAKE) infra-down
@echo "✅ All services stopped!"
# Show logs for everything
logs:
@echo "Showing logs..."
$(MAKE) infra-logs
# Restart everything
restart: down
@sleep 2
$(MAKE) up
# Show status of all containers
status:
@echo "Container status:"
@docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "rankr|NAMES"
# ====================================================================================
# Production Docker Build Commands
# ====================================================================================
.PHONY: $(SERVICES:%=docker-build-%-prod) docker-build-all-prod vendor
DOCKER_REGISTRY ?=
IMAGE_TAG ?= latest
vendor:
@echo "Updating vendor dependencies..."
go mod vendor
# ====================================================================================
# Production Service Commands
# ====================================================================================
.PHONY: $(SERVICES:%=start-%-app-prod) $(SERVICES:%=start-%-app-prod-log) $(SERVICES:%=stop-%-app-prod)
define SERVICE_PROD_template
start-$(1)-app-prod:
@echo "Starting $(1) production service..."
cd deploy/$(1)/production && PROJECT_ROOT=$(CURDIR) $(DOCKER_COMPOSE) --env-file $(CURDIR)/deploy/.env.production up -d --build
start-$(1)-app-prod-log:
@echo "Starting $(1) production service with logs..."
cd deploy/$(1)/production && PROJECT_ROOT=$(CURDIR) $(DOCKER_COMPOSE) --env-file $(CURDIR)/deploy/.env.production up --build
stop-$(1)-app-prod:
@echo "Stopping $(1) production service..."
cd deploy/$(1)/production && PROJECT_ROOT=$(CURDIR) $(DOCKER_COMPOSE) --env-file $(CURDIR)/deploy/.env.production down
endef
$(foreach svc,$(SERVICES),$(eval $(call SERVICE_PROD_template,$(svc))))
services-up-prod:
@echo "Starting all production services..."
@for svc in $(SERVICES); do \
$(MAKE) start-$$svc-app-prod; \
done
@echo "All production services are up!"
services-down-prod:
@echo "Stopping all production services..."
@for svc in $(SERVICES); do \
$(MAKE) stop-$$svc-app-prod; \
done
@echo "All production services stopped!"
define DOCKER_PROD_template
docker-build-$(1)-prod:
@echo "Building production image for $(1)..."
docker build -t $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/)rankr-$(1):$(IMAGE_TAG) \
-f deploy/$(1)/production/Dockerfile .
endef
$(foreach svc,$(SERVICES),$(eval $(call DOCKER_PROD_template,$(svc))))
docker-build-all-prod:
@echo "Building all production images..."
@set -e; \
for svc in $(SERVICES); do \
$(MAKE) docker-build-$$svc-prod; \
done
@echo "All production images built!"
# ====================================================================================
# Help
# ====================================================================================
.PHONY: help
help:
@echo "Available targets:"
@echo ""
@echo " Infrastructure (Development):"
@echo " infra-up - Start all infrastructure services"
@echo " infra-down - Stop all infrastructure services"
@echo " infra-up-postgres - Start PostgreSQL only"
@echo " infra-up-redis - Start Redis only"
@echo " infra-up-nats - Start NATS only"
@echo " infra-up-emqx - Start EMQX only"
@echo " infra-logs - Tail infrastructure logs"
@echo ""
@echo " Infrastructure (Production):"
@echo " infra-up-prod - Start production infrastructure"
@echo " infra-down-prod - Stop production infrastructure"
@echo " infra-logs-prod - Tail production infrastructure logs"
@echo " infra-status-prod - Show production infrastructure status"
@echo ""
@echo " Services Development ($(SERVICES)):"
@echo " start-<service>-app-dev - Start a single service (e.g. make start-auth-app-dev)"
@echo " start-<service>-app-dev-log - Start a service and attach logs"
@echo " stop-<service>-app-dev - Stop a single service"
@echo " services-up - Start every service listed above"
@echo " services-down - Stop every service listed above"
@echo " services-logs - Tail logs for every service (Ctrl+C to stop)"
@echo ""
@echo " Services Production:"
@echo " start-<service>-app-prod - Start a single prod service (e.g. make start-auth-app-prod)"
@echo " start-<service>-app-prod-log - Start a prod service and attach logs"
@echo " stop-<service>-app-prod - Stop a single prod service"
@echo " services-up-prod - Start all production services"
@echo " services-down-prod - Stop all production services"
@echo ""
@echo " Protobuf:"
@echo " proto-setup - Setup Buf for project"
@echo " proto-setup-full - Setup Buf and generate code"
@echo " proto-gen - Generate protobuf code"
@echo " proto-lint - Lint protobuf files"
@echo " proto-breaking - Check for breaking changes"
@echo " proto-clean - Clean generated protobuf files"
@echo " proto-format - Format protobuf files"
@echo " proto-deps - Update protobuf dependencies"
@echo " proto-validate - Validate protobuf files"
@echo ""
@echo " Build & Development:"
@echo " build - Build all Go packages"
@echo " test - Run tests"
@echo " clean - Clean build artifacts"
@echo " mod-tidy - Tidy go modules"
@echo " lint - Run linter"
@echo " install-linter - Install linter"
@echo " install-protoc-plugins - Install protoc plugins"
@echo " install-buf - Install Buf"
@echo ""
@echo " Docker:"
@echo " services-up - Start all services"
@echo " services-down - Stop all services"
@echo " services-logs - Show logs for all services"
@echo ""
@echo " Production Docker:"
@echo " vendor - Update vendor dependencies"
@echo " docker-build-<service>-prod - Build production image (e.g. make docker-build-auth-prod)"
@echo " docker-build-all-prod - Build all production images"
@echo " IMAGE_TAG=v1.0.0 - Set image tag (default: latest)"
@echo " DOCKER_REGISTRY=gcr.io/myproj - Set registry prefix"
@echo ""
@echo " Utilities:"
@echo " up - Start infrastructure and services (dev)"
@echo " down - Stop infrastructure and services (dev)"
@echo " up-prod - Start infrastructure and services (prod)"
@echo " down-prod - Stop infrastructure and services (prod)"
@echo " logs - Show logs"
@echo " restart - Restart everything"
@echo " status - Show container status (dev)"
@echo " status-prod - Show container status (prod)"