- 安装Gin框架 v1.11.0 - 重构main.go使用Gin路由器 - 更新handlers使用gin.Context - 重构middleware使用Gin中间件模式 - 更新项目文档(README.md, CLAUDE.md) 项目现已准备好进行数据库集成和前端集成 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
3.1 KiB
3.1 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
AnKao is a Go web application built using the Gin framework. The project follows a clean architecture pattern with clear separation of concerns and is designed to support database integration.
Architecture
Request Flow
- HTTP requests arrive at the Gin server running on port 8080
- Requests pass through Gin's default middleware (Logger, Recovery) and custom middleware
- The Gin router matches routes and directs requests to appropriate handlers
- Handlers (in
internal/handlers/) process requests using*gin.Contextand return JSON responses
Middleware Pattern
Middleware in Gin uses the gin.HandlerFunc pattern:
func MiddlewareName() gin.HandlerFunc {
return func(c *gin.Context) {
// Pre-processing
c.Next()
// Post-processing
}
}
Middleware is registered in main.go:15 using r.Use().
Module Structure
- main.go - Application entry point, server configuration and routing setup
- internal/handlers/ - HTTP request handlers, all use
*gin.Contextand return JSON responses - internal/middleware/ - Gin middleware chain (currently: custom logger)
- internal/models/ - Data models (directory exists, ready for database models)
- pkg/config/ - Configuration management (directory exists but not yet populated)
Common Commands
Development
# Run the server
go run main.go
# Install/update dependencies
go mod tidy
# Format code
go fmt ./...
# Vet code for common issues
go vet ./...
Building
# Build binary to bin/server
go build -o bin/server.exe main.go
# Run built binary (Windows)
.\bin\server.exe
# Run built binary (Unix)
./bin/server
Testing
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests in a specific package
go test ./internal/handlers/
# Run tests with verbose output
go test -v ./...
Key Implementation Details
- Framework: Using Gin v1.11.0
- Server Port: :8080 (configured in main.go:22)
- Handler Signature: All handlers use
func(c *gin.Context)pattern - JSON Response: Use
c.JSON()method withgin.H{}or structs - Import Paths: Use module name
ankao(defined in go.mod) - Route Registration: Routes are registered in main.go using
r.GET(),r.POST(), etc. - Middleware: Applied globally with
r.Use()or per-route with route grouping
Adding New Features
Adding a New Handler
- Create handler function in
internal/handlers/with signaturefunc(c *gin.Context) - Use
c.JSON()to return responses - Register route in main.go (e.g.,
r.GET("/path", handlers.YourHandler))
Adding Middleware
- Create middleware in
internal/middleware/returninggin.HandlerFunc - Apply globally with
r.Use(middleware.YourMiddleware())or to route groups
Database Integration
The project is ready for database integration:
- Add models in
internal/models/ - Consider using GORM or similar ORM
- Add database initialization in main.go or separate package