feat: 完成后端服务核心业务逻辑实现
## 主要功能 - ✅ 用户认证模块 (登录/注册/JWT) - ✅ 照片管理模块 (上传/查询/分页/搜索) - ✅ 分类管理模块 (创建/查询/分页) - ✅ 用户管理模块 (用户列表/分页查询) - ✅ 健康检查接口 ## 技术实现 - 基于 go-zero v1.8.0 标准架构 - Handler → Logic → Model 三层架构 - SQLite/PostgreSQL 数据库支持 - JWT 认证机制 - bcrypt 密码加密 - 统一响应格式 - 自定义模型方法 (分页/搜索) ## API 接口 - POST /api/v1/auth/login - 用户登录 - POST /api/v1/auth/register - 用户注册 - GET /api/v1/health - 健康检查 - GET /api/v1/photos - 照片列表 - POST /api/v1/photos - 上传照片 - GET /api/v1/categories - 分类列表 - POST /api/v1/categories - 创建分类 - GET /api/v1/users - 用户列表 ## 配置完成 - 开发环境配置 (SQLite) - 生产环境支持 (PostgreSQL) - JWT 认证配置 - 文件上传配置 - Makefile 构建脚本 服务已验证可正常构建和启动。
This commit is contained in:
267
backend/Makefile
267
backend/Makefile
@ -1,197 +1,102 @@
|
||||
# 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
|
||||
# 默认配置
|
||||
BINARY_NAME=photography-api
|
||||
BUILD_DIR=bin
|
||||
CONFIG_FILE=etc/photographyapi-api.yaml
|
||||
|
||||
# Color definitions
|
||||
GREEN := \033[0;32m
|
||||
YELLOW := \033[1;33m
|
||||
BLUE := \033[0;34m
|
||||
RED := \033[0;31m
|
||||
NC := \033[0m # No Color
|
||||
# 环境变量
|
||||
export GO111MODULE=on
|
||||
export GOPROXY=https://goproxy.cn,direct
|
||||
|
||||
# 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)"
|
||||
# 构建
|
||||
build:
|
||||
@echo "Building $(BINARY_NAME)..."
|
||||
@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)"
|
||||
go build -o $(BUILD_DIR)/$(BINARY_NAME) cmd/api/main.go
|
||||
|
||||
clean: ## Clean build artifacts
|
||||
@printf "$(GREEN)🧹 Cleaning build files...\n$(NC)"
|
||||
# 运行
|
||||
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)
|
||||
@rm -f coverage.out coverage.html
|
||||
@printf "$(GREEN)✅ Clean completed!\n$(NC)"
|
||||
@go clean
|
||||
|
||||
##@ 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)"
|
||||
# 代码检查
|
||||
lint:
|
||||
@echo "Running linter..."
|
||||
@golangci-lint run
|
||||
@printf "$(GREEN)✅ Linting completed!\n$(NC)"
|
||||
|
||||
test: ## Run tests
|
||||
@printf "$(GREEN)🧪 Running tests...\n$(NC)"
|
||||
# 格式化代码
|
||||
fmt:
|
||||
@echo "Formatting code..."
|
||||
@go fmt ./...
|
||||
|
||||
# 运行测试
|
||||
test:
|
||||
@echo "Running tests..."
|
||||
@go test -v ./...
|
||||
@printf "$(GREEN)✅ Tests completed!\n$(NC)"
|
||||
|
||||
##@ Utility Commands
|
||||
# 创建必要目录
|
||||
setup:
|
||||
@echo "Setting up directories..."
|
||||
@mkdir -p data uploads $(BUILD_DIR)
|
||||
|
||||
install: ## Install dependencies
|
||||
@printf "$(GREEN)📦 Installing dependencies...\n$(NC)"
|
||||
@go mod download
|
||||
@go mod tidy
|
||||
@printf "$(GREEN)✅ Dependencies installed!\n$(NC)"
|
||||
# 健康检查
|
||||
status:
|
||||
@echo "API Status:"
|
||||
@curl -s http://localhost:8080/api/v1/health || echo "API is not running"
|
||||
|
||||
logs: ## Show application logs
|
||||
@printf "$(GREEN)📄 Showing application logs...\n$(NC)"
|
||||
@docker-compose logs -f $(APP_NAME)
|
||||
# 部署准备
|
||||
deploy-prep: clean install lint test build
|
||||
@echo "Deployment preparation complete."
|
||||
|
||||
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)"
|
||||
# 显示帮助
|
||||
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 " test - Run tests"
|
||||
@echo " setup - Create necessary directories"
|
||||
@echo " status - Check API status"
|
||||
@echo " deploy-prep - Prepare for deployment"
|
||||
@echo " help - Show this help message"
|
||||
|
||||
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)"
|
||||
.PHONY: build run dev quick install gen gen-model clean lint fmt test setup status deploy-prep help
|
||||
Reference in New Issue
Block a user