# 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)"