# go-zero Development Rules You are an expert in go-zero microservices framework development. ## Key Principles Follow these go-zero patterns strictly: ### Architecture - **Three-layer separation**: Handler (HTTP) → Logic (business) → Model (data) - Never put business logic in handlers - Always use ServiceContext for dependency injection - Use GORM as ORM for database operations ### Code Generation - Use `goctl` for code generation, never hand-write boilerplate - API definitions go in `.api` files - RPC definitions go in `.proto` files ### Error Handling - Use custom `response.Success(w, data)` for success responses - Use custom `response.Error(w, err)` for error responses - Use `errorx.CodeError` for business errors with error codes - Never use `fmt.Fprintf()` or `w.Write()` directly ### Configuration - Load config with `conf.MustLoad(&c, *configFile)` - Never hard-code ports, hosts, or credentials - Use environment-specific config files ### Context Propagation - Always pass `ctx context.Context` through all layers - Use context for tracing, cancellation, and timeouts ### Database (GORM) - Use GORM for all database operations - Define models in `internal/model/` directory - Initialize DB connection in ServiceContext - Use soft delete with `gorm.DeletedAt` field ## Pattern References When I need detailed patterns, I'll reference these files: - REST APIs: .ai-context/zero-skills/references/rest-api-patterns.md - RPC services: .ai-context/zero-skills/references/rpc-patterns.md - Database: .ai-context/zero-skills/references/database-patterns.md - Resilience: .ai-context/zero-skills/references/resilience-patterns.md - Troubleshooting: .ai-context/zero-skills/troubleshooting/common-issues.md ## Project Structure ``` backend/healthapi/ ├── healthapi.api # API definitions ├── healthapi.go # Entry point ├── internal/ │ ├── config/ # Configuration │ ├── handler/ # HTTP handlers │ ├── logic/ # Business logic │ ├── model/ # GORM models │ ├── svc/ # ServiceContext │ └── types/ # Generated types ├── pkg/ │ ├── errorx/ # Custom errors │ └── response/ # Unified response └── etc/ └── healthapi.yaml # Config file ``` ## Common Commands ```bash # Generate API code goctl api go -api healthapi.api -dir . # Generate model from database (if needed) goctl model mysql datasource -url="user:pass@tcp(localhost:3306)/db" -table="users" -dir="./model" # Run the service go run healthapi.go -f etc/healthapi.yaml ``` ## AI Service Integration This project integrates with Aliyun Dashscope (Qwen models) for AI chat functionality: - AI client initialized in ServiceContext - SSE streaming support for real-time responses - System prompt includes user health profile data