-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
260 lines (208 loc) · 9.52 KB
/
Makefile
File metadata and controls
260 lines (208 loc) · 9.52 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
# Makefile
.PHONY: help run build test migrate-up migrate-down migrate-create migrate-version migrate-force migrate-force-go docker-build docker-clean docker-clean-all docker-up docker-down
ENV_FILE := env/.env
ifneq (,$(wildcard $(ENV_FILE)))
include $(ENV_FILE)
export
endif
# Variable Setting
APP_NAME := gamers-api
BUILD_DIR := ./bin
MIGRATIONS_PATH ?= ./db/migrations
DB_URL := mysql://$(DB_USER):$(DB_PASSWORD)@tcp($(DB_HOST):$(DB_PORT))/$(DB_NAME)
# ========================================
# Basic Command
# ========================================
help: ## Show this help message
@echo "GAMERS Backend - Available commands:"
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}'
run: ## Run the application
@echo "🚀 Starting GAMERS API..."
RUN_MIGRATIONS=true go run ./cmd/server.go
run-no-migrate: ## Run without migrations
@echo "🚀 Starting GAMERS API (no migrations)..."
go run ./cmd/server.go
build: ## Build the application
@echo "🔨 Building $(APP_NAME)..."
@mkdir -p $(BUILD_DIR)
go build -o $(BUILD_DIR)/$(APP_NAME) ./cmd/server.go
@echo "✅ Build complete: $(BUILD_DIR)/$(APP_NAME)"
test: ## Run tests
@echo "🧪 Running tests..."
go test -v ./...
test-coverage: ## Run tests with coverage
@echo "🧪 Running tests with coverage..."
go test -v -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
@echo "✅ Coverage report: coverage.html"
clean: ## Clean build artifacts
@echo "🧹 Cleaning..."
rm -rf $(BUILD_DIR)
rm -f coverage.out coverage.html
go clean
# ========================================
# Migration 명령어
# ========================================
migrate-up: ## Run all pending migrations
@echo "🔄 Running migrations..."
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" up
migrate-down: ## Rollback last migration
@echo "⏪ Rolling back last migration..."
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" down 1
migrate-down-all: ## Rollback all migrations
@echo "⏪ Rolling back all migrations..."
@read -p "Are you sure? [y/N] " -n 1 -r; \
echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" down -all; \
fi
migrate-create: ## Create new migration (usage: make migrate-create name=create_users_table)
@if [ -z "$(name)" ]; then \
echo "❌ Error: name parameter is required"; \
echo "Usage: make migrate-create name=create_users_table"; \
exit 1; \
fi
@echo "📝 Creating migration: $(name)"
migrate create -ext sql -dir $(MIGRATIONS_PATH) -seq $(name)
migrate-version: ## Show current migration version
@migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" version
migrate-force: ## Force set migration version using migrate CLI (usage: make migrate-force version=1)
@if [ -z "$(version)" ]; then \
echo "❌ Error: version parameter is required"; \
echo "Usage: make migrate-force version=1"; \
exit 1; \
fi
@echo "⚠️ Forcing migration version to $(version)..."
migrate -path $(MIGRATIONS_PATH) -database "$(DB_URL)" force $(version)
migrate-force-go: ## Force set migration version using Go code (usage: make migrate-force-go version=1)
@if [ -z "$(version)" ]; then \
echo "❌ Error: version parameter is required"; \
echo "Usage: make migrate-force-go version=1"; \
exit 1; \
fi
@echo "⚠️ Forcing migration version to $(version) using Go..."
go run scripts/force-migration.go $(version)
# ========================================
# Docker Command
# ========================================
docker-build: ## Build Docker image
@echo "🐳 Building Docker image..."
docker compose -p gamers-web-server -f ./docker/docker-compose.yaml build
@echo "🧹 Cleaning up dangling images..."
@docker image prune -f
@echo "✅ Docker build complete"
docker-clean: ## Remove dangling images and unused resources
@echo "🧹 Removing dangling images..."
@docker image prune -f
@echo "✅ Dangling images removed"
docker-clean-all: ## Remove all unused images, containers, and volumes (use with caution)
@echo "⚠️ Removing all unused Docker resources..."
@docker system prune -f
@echo "✅ Cleanup complete"
docker-up: ## Start Docker containers
@echo "🐳 Starting Docker containers..."
docker compose -p gamers-web-server -f ./docker/docker-compose.yaml up -d
docker-down: ## Stop Docker containers
@echo "🐳 Stopping Docker containers..."
docker compose -p gamers-web-server -f ./docker/docker-compose.yaml down
docker-logs: ## Show Docker logs
docker compose -p gamers-web-server -f ./docker/docker-compose.yaml logs -f app
docker-restart: docker down docker-up ## Restart Docker containers
# Docker Network 기반 Migration (gamers-network 내에서 일회성 컨테이너로 실행)
MIGRATE_DOCKER := docker run --rm --network gamers-network \
-v $(PWD)/db/migrations:/migrations \
migrate/migrate \
-path=/migrations \
-database "mysql://$(DB_USER):$(DB_PASSWORD)@tcp(gamers-mysql:$(DB_PORT))/$(DB_NAME)"
migrate-up-network: ## Run migrations via Docker network
@echo "🔄 Running migrations via Docker network..."
$(MIGRATE_DOCKER) up
migrate-down-network: ## Rollback last migration via Docker network
@echo "⏪ Rolling back last migration via Docker network..."
$(MIGRATE_DOCKER) down 1
migrate-version-network: ## Show migration version via Docker network
@echo "📊 Current migration version:"
@$(MIGRATE_DOCKER) version
migrate-force-network: ## Force set migration version or fix dirty state via Docker network (usage: make migrate-force-network version=3)
@if [ -z "$(version)" ]; then \
echo "❌ Error: version parameter is required"; \
echo "Usage: make migrate-force-network version=3"; \
echo ""; \
echo "💡 Tip: This also fixes dirty migration state"; \
exit 1; \
fi
@echo "🔧 Forcing migration version to $(version) via Docker network..."
$(MIGRATE_DOCKER) force $(version)
@echo "✅ Migration version set to $(version)"
# Legacy: Direct MySQL access (fallback)
migrate-force-docker: ## Force set migration version via Docker MySQL (usage: make migrate-force-docker version=3)
@if [ -z "$(version)" ]; then \
echo "❌ Error: version parameter is required"; \
echo "Usage: make migrate-force-docker version=3"; \
exit 1; \
fi
@echo "🔧 Forcing migration version to $(version) via Docker..."
@docker exec gamers-mysql mysql -u$(DB_USER) -p$(DB_PASSWORD) $(DB_NAME) -e "UPDATE schema_migrations SET version=$(version), dirty=0;"
@echo "✅ Migration version set to $(version)"
migrate-status-docker: ## Show migration status via Docker MySQL
@echo "📊 Current migration status:"
@docker exec gamers-mysql mysql -u$(DB_USER) -p$(DB_PASSWORD) $(DB_NAME) -e "SELECT * FROM schema_migrations;"
# ========================================
# Development Tools
# ========================================
swagger: ## Generate swagger documentation
@echo "📚 Generating Swagger docs..."
swag init -g cmd/server.go --output docs --parseDependency --parseInternal
deps: ## Download dependencies
@echo "📦 Downloading dependencies..."
go mod download
go mod tidy
redis-dump: ## Show all Redis keys
@echo "🔍 Redis keys:"
@redis-cli -h $(REDIS_HOST) -p $(REDIS_PORT) -a $(REDIS_PASSWORD) -n $(REDIS_DB) --scan --pattern "*" 2>/dev/null | while read key; do \
value=$$(redis-cli -h $(REDIS_HOST) -p $(REDIS_PORT) -a $(REDIS_PASSWORD) -n $(REDIS_DB) GET "$$key" 2>/dev/null); \
ttl=$$(redis-cli -h $(REDIS_HOST) -p $(REDIS_PORT) -a $(REDIS_PASSWORD) -n $(REDIS_DB) TTL "$$key" 2>/dev/null); \
echo "Key: $$key"; \
echo "Value: $$value"; \
echo "TTL: $$ttl seconds"; \
echo "---"; \
done
redis-clear: ## Clear Redis database
@echo "🗑️ Clearing Redis..."
@redis-cli -h $(REDIS_HOST) -p $(REDIS_PORT) -a $(REDIS_PASSWORD) -n $(REDIS_DB) FLUSHDB
@echo "✅ Redis cleared"
# ========================================
# Deployment Commands
# ========================================
docker-login-ghcr: ## Login to GitHub Container Registry
@echo "🔐 Logging in to GitHub Container Registry..."
@read -p "GitHub Username: " username; \
read -sp "GitHub Token: " token; \
echo; \
echo $$token | docker login ghcr.io -u $$username --password-stdin
docker-push: ## Build and push Docker image to GHCR
@echo "🚀 Building and pushing Docker image..."
@if [ -z "$(TAG)" ]; then \
echo "Usage: make docker-push TAG=v1.0.0"; \
exit 1; \
fi
docker build -f docker/Dockerfile -t ghcr.io/$(shell git config --get remote.origin.url | sed 's/.*://;s/.git$$//' | tr '[:upper:]' '[:lower:]'):$(TAG) .
docker push ghcr.io/$(shell git config --get remote.origin.url | sed 's/.*://;s/.git$$//' | tr '[:upper:]' '[:lower:]'):$(TAG)
docker-pull: ## Pull Docker image from GHCR
@echo "⬇️ Pulling Docker image from GHCR..."
@if [ -z "$(TAG)" ]; then TAG=latest; fi
docker pull ghcr.io/$(shell git config --get remote.origin.url | sed 's/.*://;s/.git$$//' | tr '[:upper:]' '[:lower:]'):$(TAG)
check-deploy: ## Check deployment prerequisites
@echo "🔍 Checking deployment prerequisites..."
@command -v docker >/dev/null 2>&1 || { echo "❌ Docker is not installed"; exit 1; }
@command -v git >/dev/null 2>&1 || { echo "❌ Git is not installed"; exit 1; }
@echo "✅ All prerequisites met"
# ========================================
# Integration Command
# ========================================
setup: deps migrate-up ## Setup project (download deps + run migrations)
@echo "✅ Project setup complete"
dev: docker-up run ## Start development environment (Docker + App)
rebuild: clean build ## Clean and rebuild
all: clean deps build test ## Run all tasks
.DEFAULT_GOAL := help