Files
photography/backend-old/Makefile
xujiang 604b9e59ba fix
2025-07-10 18:09:11 +08:00

197 lines
7.6 KiB
Makefile

# Photography Backend Makefile
# Simple and functional Makefile for Go backend project with Docker support
.PHONY: help dev dev-up dev-down build clean docker-build docker-run prod-up prod-down status health fmt mod
# Color definitions
GREEN := \033[0;32m
YELLOW := \033[1;33m
BLUE := \033[0;34m
RED := \033[0;31m
NC := \033[0m # No Color
# Application configuration
APP_NAME := photography-backend
VERSION := 1.0.0
BUILD_TIME := $(shell date +%Y%m%d_%H%M%S)
LDFLAGS := -X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)
# Build configuration
BUILD_DIR := bin
MAIN_FILE := cmd/server/main.go
# Database configuration
DB_URL := postgres://postgres:password@localhost:5432/photography?sslmode=disable
MIGRATION_DIR := migrations
# Default target
.DEFAULT_GOAL := help
##@ Development Environment Commands
dev: ## Start development server with SQLite database
@printf "$(GREEN)🚀 Starting development server with SQLite...\n$(NC)"
@go run cmd/server/main_with_db.go
dev-simple: ## Start simple development server (mock data)
@printf "$(GREEN)🚀 Starting simple development server...\n$(NC)"
@go run cmd/server/simple_main.go
dev-up: ## Start development environment with Docker
@printf "$(GREEN)🐳 Starting development environment...\n$(NC)"
@docker-compose -f docker-compose.dev.yml up -d
@printf "$(GREEN)✅ Development environment started successfully!\n$(NC)"
dev-down: ## Stop development environment
@printf "$(GREEN)🛑 Stopping development environment...\n$(NC)"
@docker-compose -f docker-compose.dev.yml down
@printf "$(GREEN)✅ Development environment stopped!\n$(NC)"
##@ Build Commands
build: ## Build the Go application
@printf "$(GREEN)🔨 Building $(APP_NAME)...\n$(NC)"
@mkdir -p $(BUILD_DIR)
@CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_FILE)
@printf "$(GREEN)✅ Build completed: $(BUILD_DIR)/$(APP_NAME)\n$(NC)"
clean: ## Clean build artifacts
@printf "$(GREEN)🧹 Cleaning build files...\n$(NC)"
@rm -rf $(BUILD_DIR)
@rm -f coverage.out coverage.html
@printf "$(GREEN)✅ Clean completed!\n$(NC)"
##@ Docker Commands
docker-build: ## Build Docker image
@printf "$(GREEN)🐳 Building Docker image...\n$(NC)"
@docker build -t $(APP_NAME):$(VERSION) .
@docker tag $(APP_NAME):$(VERSION) $(APP_NAME):latest
@printf "$(GREEN)✅ Docker image built: $(APP_NAME):$(VERSION)\n$(NC)"
docker-run: ## Run application in Docker container
@printf "$(GREEN)🐳 Running Docker container...\n$(NC)"
@docker-compose up -d
@printf "$(GREEN)✅ Docker container started!\n$(NC)"
##@ Production Commands
prod-up: ## Start production environment
@printf "$(GREEN)🚀 Starting production environment...\n$(NC)"
@docker-compose up -d
@printf "$(GREEN)✅ Production environment started!\n$(NC)"
prod-down: ## Stop production environment
@printf "$(GREEN)🛑 Stopping production environment...\n$(NC)"
@docker-compose down
@printf "$(GREEN)✅ Production environment stopped!\n$(NC)"
##@ Health Check & Status Commands
status: ## Check application and services status
@printf "$(GREEN)📊 Checking application status...\n$(NC)"
@printf "$(BLUE)Docker containers:$(NC)\n"
@docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "($(APP_NAME)|postgres|redis)" || echo "No containers running"
@printf "$(BLUE)Application build:$(NC)\n"
@if [ -f "$(BUILD_DIR)/$(APP_NAME)" ]; then \
printf "$(GREEN)✅ Binary exists: $(BUILD_DIR)/$(APP_NAME)\n$(NC)"; \
ls -lh $(BUILD_DIR)/$(APP_NAME); \
else \
printf "$(RED)❌ Binary not found. Run 'make build' first.\n$(NC)"; \
fi
health: ## Check health of running services
@printf "$(GREEN)🏥 Checking service health...\n$(NC)"
@printf "$(BLUE)Testing application endpoint...\n$(NC)"
@curl -f http://localhost:8080/health 2>/dev/null && printf "$(GREEN)✅ Application is healthy\n$(NC)" || printf "$(RED)❌ Application is not responding\n$(NC)"
@printf "$(BLUE)Database connection...\n$(NC)"
@docker exec photography-postgres pg_isready -U postgres 2>/dev/null && printf "$(GREEN)✅ Database is ready\n$(NC)" || printf "$(RED)❌ Database is not ready\n$(NC)"
##@ Code Quality Commands
fmt: ## Format Go code
@printf "$(GREEN)🎨 Formatting Go code...\n$(NC)"
@go fmt ./...
@printf "$(GREEN)✅ Code formatted!\n$(NC)"
mod: ## Tidy Go modules
@printf "$(GREEN)📦 Tidying Go modules...\n$(NC)"
@go mod tidy
@go mod download
@printf "$(GREEN)✅ Modules tidied!\n$(NC)"
lint: ## Run code linter
@printf "$(GREEN)🔍 Running linter...\n$(NC)"
@golangci-lint run
@printf "$(GREEN)✅ Linting completed!\n$(NC)"
test: ## Run tests
@printf "$(GREEN)🧪 Running tests...\n$(NC)"
@go test -v ./...
@printf "$(GREEN)✅ Tests completed!\n$(NC)"
##@ Utility Commands
install: ## Install dependencies
@printf "$(GREEN)📦 Installing dependencies...\n$(NC)"
@go mod download
@go mod tidy
@printf "$(GREEN)✅ Dependencies installed!\n$(NC)"
logs: ## Show application logs
@printf "$(GREEN)📄 Showing application logs...\n$(NC)"
@docker-compose logs -f $(APP_NAME)
migrate-up: ## Run database migrations
@printf "$(GREEN)🗄️ Running database migrations...\n$(NC)"
@migrate -path $(MIGRATION_DIR) -database "$(DB_URL)" up
@printf "$(GREEN)✅ Migrations completed!\n$(NC)"
migrate-down: ## Rollback database migrations
@printf "$(GREEN)🗄️ Rolling back database migrations...\n$(NC)"
@migrate -path $(MIGRATION_DIR) -database "$(DB_URL)" down
@printf "$(GREEN)✅ Migrations rolled back!\n$(NC)"
##@ Database Commands
db-reset: ## Reset SQLite database (delete and recreate)
@printf "$(GREEN)🗄️ Resetting SQLite database...\n$(NC)"
@rm -f photography.db
@printf "$(GREEN)✅ Database reset! Will be recreated on next startup.\n$(NC)"
db-backup: ## Backup SQLite database
@printf "$(GREEN)💾 Backing up SQLite database...\n$(NC)"
@cp photography.db photography_backup_$(BUILD_TIME).db
@printf "$(GREEN)✅ Database backed up to photography_backup_$(BUILD_TIME).db\n$(NC)"
db-shell: ## Open SQLite database shell
@printf "$(GREEN)🐚 Opening SQLite database shell...\n$(NC)"
@sqlite3 photography.db
db-status: ## Show database status and table info
@printf "$(GREEN)📊 Database status:\n$(NC)"
@if [ -f "photography.db" ]; then \
printf "$(BLUE)Database file: photography.db ($(shell ls -lh photography.db | awk '{print $$5}'))\\n$(NC)"; \
printf "$(BLUE)Tables:\\n$(NC)"; \
sqlite3 photography.db ".tables"; \
printf "$(BLUE)Row counts:\\n$(NC)"; \
sqlite3 photography.db "SELECT 'Users: ' || COUNT(*) FROM users; SELECT 'Photos: ' || COUNT(*) FROM photos; SELECT 'Categories: ' || COUNT(*) FROM categories; SELECT 'Tags: ' || COUNT(*) FROM tags;"; \
else \
printf "$(RED)❌ Database not found. Run 'make dev' to create it.\\n$(NC)"; \
fi
##@ Help
help: ## Display this help message
@printf "$(GREEN)Photography Backend Makefile\n$(NC)"
@printf "$(GREEN)============================\n$(NC)"
@printf "$(YELLOW)Simple and functional Makefile for Go backend project with Docker support\n$(NC)\n"
@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*?##/ { printf "$(BLUE)%-15s$(NC) %s\n", $$1, $$2 } /^##@/ { printf "\n$(GREEN)%s\n$(NC)", substr($$0, 5) } ' $(MAKEFILE_LIST)
@printf "\n$(YELLOW)Examples:\n$(NC)"
@printf "$(BLUE) make dev$(NC) - Start development server\n"
@printf "$(BLUE) make dev-up$(NC) - Start development environment\n"
@printf "$(BLUE) make build$(NC) - Build the application\n"
@printf "$(BLUE) make docker-build$(NC) - Build Docker image\n"
@printf "$(BLUE) make status$(NC) - Check application status\n"
@printf "$(BLUE) make health$(NC) - Check service health\n"
@printf "\n$(GREEN)For more information, visit: https://github.com/iriver/photography\n$(NC)"