## 🧪 API测试系统完善 - 创建完整的单元测试套件 (tests/unit_test.go) - 认证流程、CRUD操作、文件上传测试 - 中间件、错误处理、性能测试 - 创建集成测试套件 (tests/integration_test.go) - 业务流程、数据一致性、并发测试 - 创建综合API测试 (test_api_comprehensive.http) - 92个测试场景,覆盖所有API端点 - 更新Makefile添加测试命令 - test-unit, test-integration, test-api, test-cover, test-bench ## 🗄️ 生产环境数据库配置 - Docker Compose生产环境配置 (configs/docker/docker-compose.prod.yml) - PostgreSQL 16 + Redis 7 + Nginx + 监控栈 - 数据库初始化脚本 (configs/docker/init-db.sql) - 完整表结构、索引优化、触发器、视图 - 生产环境配置脚本 (scripts/production-db-setup.sh) - 自动化配置、连接池、备份策略、监控 ## 📚 API文档完善 - 完整的API文档 (docs/API_DOCUMENTATION.md) - 详细接口说明、请求响应示例 - 认证流程、错误处理、性能优化 - SDK支持、部署指南、安全考虑 - 包含cURL示例和Postman Collection支持 ## 📊 项目进度 - 总进度: 50.0% → 57.5% - 中优先级任务: 55% → 70% - 并行完成3个重要任务,显著提升项目完成度 ## 🎯 技术成果 - 测试覆盖率大幅提升,支持自动化测试 - 生产环境就绪,支持Docker部署 - 完整的API文档,便于前后端协作 - 性能优化和监控配置,确保生产稳定性
259 lines
7.3 KiB
Makefile
259 lines
7.3 KiB
Makefile
# Photography Backend Makefile
|
|
|
|
# 默认配置
|
|
BINARY_NAME=photography-api
|
|
BUILD_DIR=bin
|
|
CONFIG_FILE=etc/photographyapi-api.yaml
|
|
|
|
# 环境变量
|
|
export GO111MODULE=on
|
|
export GOPROXY=https://goproxy.cn,direct
|
|
|
|
# 构建
|
|
build:
|
|
@echo "Building $(BINARY_NAME)..."
|
|
@mkdir -p $(BUILD_DIR)
|
|
go build -o $(BUILD_DIR)/$(BINARY_NAME) cmd/api/main.go
|
|
|
|
# 运行
|
|
run:
|
|
@echo "Running $(BINARY_NAME)..."
|
|
@./$(BUILD_DIR)/$(BINARY_NAME) -f $(CONFIG_FILE)
|
|
|
|
# 开发模式(构建并运行)
|
|
dev: build run
|
|
|
|
# 快速启动(跳过构建)
|
|
quick:
|
|
@echo "Quick start $(BINARY_NAME)..."
|
|
@go run cmd/api/main.go -f $(CONFIG_FILE)
|
|
|
|
# 安装依赖
|
|
install:
|
|
@echo "Installing dependencies..."
|
|
@go mod tidy
|
|
|
|
# 代码生成
|
|
gen:
|
|
@echo "Generating code..."
|
|
@goctl api go -api api/desc/photography.api -dir ./ --style=goZero
|
|
|
|
# 生成模型
|
|
gen-model:
|
|
@echo "Generating models..."
|
|
@goctl model mysql ddl -src internal/model/sql/user.sql -dir internal/model/
|
|
@goctl model mysql ddl -src internal/model/sql/photo.sql -dir internal/model/
|
|
@goctl model mysql ddl -src internal/model/sql/category.sql -dir internal/model/
|
|
|
|
# 清理
|
|
clean:
|
|
@echo "Cleaning..."
|
|
@rm -rf $(BUILD_DIR)
|
|
@go clean
|
|
|
|
# 代码检查
|
|
lint:
|
|
@echo "Running linter..."
|
|
@golangci-lint run
|
|
|
|
# 格式化代码
|
|
fmt:
|
|
@echo "Formatting code..."
|
|
@go fmt ./...
|
|
|
|
# 运行测试
|
|
test:
|
|
@echo "Running tests..."
|
|
@go test -v ./...
|
|
|
|
# 运行单元测试
|
|
test-unit:
|
|
@echo "Running unit tests..."
|
|
@go test -v -short ./...
|
|
|
|
# 运行集成测试
|
|
test-integration:
|
|
@echo "Running integration tests..."
|
|
@go test -v -tags=integration ./tests/...
|
|
|
|
# 运行 API 测试
|
|
test-api:
|
|
@echo "Running API tests..."
|
|
@if pgrep -f "photography-api" > /dev/null; then \
|
|
echo "Server is running, starting API tests..."; \
|
|
go test -v -tags=api ./tests/...; \
|
|
else \
|
|
echo "Error: Server is not running. Please start the server first."; \
|
|
exit 1; \
|
|
fi
|
|
|
|
# 生成测试覆盖率报告
|
|
test-cover:
|
|
@echo "Generating test coverage report..."
|
|
@go test -coverprofile=coverage.out ./...
|
|
@go tool cover -html=coverage.out -o coverage.html
|
|
@echo "Coverage report generated: coverage.html"
|
|
|
|
# 运行基准测试
|
|
test-bench:
|
|
@echo "Running benchmark tests..."
|
|
@go test -bench=. -benchmem ./...
|
|
|
|
# 运行所有测试
|
|
test-all: test-unit test-integration test-bench test-cover
|
|
@echo "All tests completed."
|
|
|
|
# 创建必要目录
|
|
setup:
|
|
@echo "Setting up directories..."
|
|
@mkdir -p data uploads $(BUILD_DIR)
|
|
|
|
# 健康检查
|
|
status:
|
|
@echo "API Status:"
|
|
@curl -s http://localhost:8080/api/v1/health || echo "API is not running"
|
|
|
|
# 种子数据管理
|
|
seed:
|
|
@echo "Running seed data..."
|
|
@./run_seed_data.sh
|
|
|
|
# 测试种子数据
|
|
test-seed:
|
|
@echo "Testing seed data..."
|
|
@./test_seed_data.sh
|
|
|
|
# 检查数据库状态
|
|
db-status:
|
|
@echo "Database Status:"
|
|
@if [ -f "./data/photography.db" ]; then \
|
|
echo "Database exists"; \
|
|
echo "User count: $$(sqlite3 ./data/photography.db 'SELECT COUNT(*) FROM user;')"; \
|
|
echo "Category count: $$(sqlite3 ./data/photography.db 'SELECT COUNT(*) FROM category;')"; \
|
|
echo "Photo count: $$(sqlite3 ./data/photography.db 'SELECT COUNT(*) FROM photo;')"; \
|
|
else \
|
|
echo "Database not found"; \
|
|
fi
|
|
|
|
# 重置数据库
|
|
db-reset:
|
|
@echo "Resetting database..."
|
|
@rm -f ./data/photography.db
|
|
@echo "Database reset complete. Run 'make quick' to recreate."
|
|
|
|
# 数据库迁移相关命令
|
|
migrate-status:
|
|
@echo "Checking migration status..."
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c status
|
|
|
|
migrate-up:
|
|
@echo "Running all pending migrations..."
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c up
|
|
|
|
migrate-down:
|
|
@echo "Rolling back migrations..."
|
|
@if [ -z "$(STEPS)" ]; then \
|
|
echo "Error: Please specify STEPS=n"; \
|
|
exit 1; \
|
|
fi
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c down -s $(STEPS)
|
|
|
|
migrate-reset:
|
|
@echo "WARNING: This will reset the entire database!"
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c reset
|
|
|
|
migrate-create:
|
|
@echo "Creating new migration..."
|
|
@if [ -z "$(NAME)" ]; then \
|
|
echo "Error: Please specify NAME=migration_name"; \
|
|
exit 1; \
|
|
fi
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c create "$(NAME)"
|
|
|
|
migrate-version:
|
|
@echo "Latest migration version:"
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c version
|
|
|
|
# 数据库初始化(全新安装)
|
|
db-init: setup
|
|
@echo "Initializing database with migrations..."
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c up
|
|
@echo "Database initialized successfully!"
|
|
|
|
# 数据库迁移(生产环境)
|
|
db-migrate:
|
|
@echo "Running production migrations..."
|
|
@go run cmd/migrate/main.go -f $(CONFIG_FILE) -c migrate
|
|
|
|
# 数据库备份
|
|
db-backup:
|
|
@echo "Creating database backup..."
|
|
@mkdir -p data/backups
|
|
@cp data/photography.db data/backups/photography_$$(date +%Y%m%d_%H%M%S).db
|
|
@echo "Backup created in data/backups/"
|
|
|
|
# 数据库恢复
|
|
db-restore:
|
|
@echo "Restoring database from backup..."
|
|
@if [ -z "$(BACKUP)" ]; then \
|
|
echo "Error: Please specify BACKUP=filename"; \
|
|
echo "Available backups:"; \
|
|
ls -la data/backups/; \
|
|
exit 1; \
|
|
fi
|
|
@if [ -f "data/backups/$(BACKUP)" ]; then \
|
|
cp "data/backups/$(BACKUP)" data/photography.db; \
|
|
echo "Database restored from $(BACKUP)"; \
|
|
else \
|
|
echo "Error: Backup file not found: $(BACKUP)"; \
|
|
exit 1; \
|
|
fi
|
|
|
|
# 部署准备
|
|
deploy-prep: clean install lint test build
|
|
@echo "Deployment preparation complete."
|
|
|
|
# 显示帮助
|
|
help:
|
|
@echo "Available commands:"
|
|
@echo " build - Build the application"
|
|
@echo " run - Run the application"
|
|
@echo " dev - Build and run in development mode"
|
|
@echo " quick - Quick start without building"
|
|
@echo " install - Install dependencies"
|
|
@echo " gen - Generate API code"
|
|
@echo " gen-model - Generate model code"
|
|
@echo " clean - Clean build artifacts"
|
|
@echo " lint - Run code linter"
|
|
@echo " fmt - Format code"
|
|
@echo " setup - Create necessary directories"
|
|
@echo " status - Check API status"
|
|
@echo " seed - Run seed data script"
|
|
@echo " test-seed - Test seed data integrity"
|
|
@echo " db-status - Check database status"
|
|
@echo " db-reset - Reset database"
|
|
@echo " deploy-prep - Prepare for deployment"
|
|
@echo ""
|
|
@echo "Testing Commands:"
|
|
@echo " test - Run all tests"
|
|
@echo " test-unit - Run unit tests only"
|
|
@echo " test-integration - Run integration tests"
|
|
@echo " test-api - Run API tests (requires server running)"
|
|
@echo " test-cover - Generate test coverage report"
|
|
@echo " test-bench - Run benchmark tests"
|
|
@echo " test-all - Run comprehensive test suite"
|
|
@echo ""
|
|
@echo "Database Migration Commands:"
|
|
@echo " migrate-status - Show migration status"
|
|
@echo " migrate-up - Apply all pending migrations"
|
|
@echo " migrate-down STEPS=n - Rollback n migrations"
|
|
@echo " migrate-reset - Reset database (WARNING: destructive)"
|
|
@echo " migrate-create NAME=name - Create new migration template"
|
|
@echo " migrate-version - Show latest migration version"
|
|
@echo " db-init - Initialize database with migrations"
|
|
@echo " db-migrate - Run production migrations"
|
|
@echo " db-backup - Create database backup"
|
|
@echo " db-restore BACKUP=file - Restore from backup"
|
|
@echo " help - Show this help message"
|
|
|
|
.PHONY: build run dev quick install gen gen-model clean lint fmt test test-unit test-integration test-api test-cover test-bench test-all setup status seed test-seed db-status db-reset deploy-prep help |