From 0361f207dbda3726ad59222f02492e323177be49 Mon Sep 17 00:00:00 2001 From: yanlongqi Date: Mon, 3 Nov 2025 11:12:37 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 37 +++++++++++++++++ CLAUDE.md | 78 +++++++++++++++++++++++++++++++++++ README.md | 62 ++++++++++++++++++++++++++++ go.mod | 3 ++ internal/handlers/handlers.go | 26 ++++++++++++ internal/middleware/logger.go | 26 ++++++++++++ main.go | 28 +++++++++++++ 7 files changed, 260 insertions(+) create mode 100644 .gitignore create mode 100644 CLAUDE.md create mode 100644 README.md create mode 100644 go.mod create mode 100644 internal/handlers/handlers.go create mode 100644 internal/middleware/logger.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b33570 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +# 二进制文件 +bin/ +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# 测试覆盖率文件 +*.out +*.test + +# IDE 配置 +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# 操作系统文件 +.DS_Store +Thumbs.db + +# 依赖目录 +vendor/ + +# 环境配置文件 +.env +.env.local + +# 日志文件 +*.log +logs/ + +# 临时文件 +tmp/ +temp/ diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..fc4e453 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,78 @@ +# 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 standard library's `net/http` package. The project follows a clean architecture pattern with clear separation of concerns. + +## Architecture + +### Request Flow +1. HTTP requests arrive at the server running on port 8080 +2. Requests pass through the `Logger` middleware (in `internal/middleware/`) +3. The router (`http.NewServeMux`) directs requests to appropriate handlers +4. Handlers (in `internal/handlers/`) process requests and return JSON responses + +### Middleware Pattern +Middleware functions wrap the main handler using the standard middleware pattern: +```go +func Middleware(next http.Handler) http.Handler +``` +Middleware is applied in [cmd/server/main.go:20](cmd/server/main.go#L20) before starting the server. + +### Module Structure +- **cmd/server/** - Application entry point, server configuration and routing setup +- **internal/handlers/** - HTTP request handlers, all return JSON responses +- **internal/middleware/** - HTTP middleware chain (currently: logging) +- **internal/models/** - Data models (directory exists but not yet populated) +- **pkg/config/** - Configuration management (directory exists but not yet populated) + +## Common Commands + +### Development +```bash +# Run the server +go run cmd/server/main.go + +# Install/update dependencies +go mod tidy + +# Format code +go fmt ./... + +# Vet code for common issues +go vet ./... +``` + +### Building +```bash +# Build binary to bin/server +go build -o bin/server cmd/server/main.go + +# Run built binary (Windows) +.\bin\server.exe + +# Run built binary (Unix) +./bin/server +``` + +### Testing +```bash +# Run all tests +go test ./... + +# Run tests with coverage +go test -cover ./... + +# Run tests in a specific package +go test ./internal/handlers/ +``` + +## Key Implementation Details + +- The server listens on port **:8080** (hardcoded in main.go:23) +- All responses are JSON with appropriate Content-Type headers +- Import paths use the module name `ankao` (defined in go.mod) +- When adding new handlers, register them in [cmd/server/main.go](cmd/server/main.go) using `mux.HandleFunc()` +- Middleware wraps the entire mux, so it applies to all routes diff --git a/README.md b/README.md new file mode 100644 index 0000000..4bcc3fc --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# AnKao Web 项目 + +这是一个使用Go语言构建的Web应用项目。 + +## 项目结构 + +``` +AnKao/ +├── cmd/ +│ └── server/ # 主程序入口 +│ └── main.go +├── internal/ # 私有应用代码 +│ ├── handlers/ # HTTP请求处理器 +│ ├── middleware/ # 中间件 +│ └── models/ # 数据模型 +├── pkg/ # 公共库代码 +│ └── config/ # 配置管理 +├── go.mod # Go模块文件 +└── README.md # 项目说明 +``` + +## 快速开始 + +### 运行服务器 + +```bash +go run cmd/server/main.go +``` + +服务器将在 `http://localhost:8080` 启动 + +### API端点 + +- `GET /` - 首页,返回欢迎信息 +- `GET /api/health` - 健康检查端点 + +## 开发 + +### 安装依赖 + +```bash +go mod tidy +``` + +### 构建 + +```bash +go build -o bin/server cmd/server/main.go +``` + +### 运行编译后的程序 + +```bash +./bin/server +``` + +## 特性 + +- 基于标准库的HTTP服务器 +- 日志中间件 +- RESTful API结构 +- 健康检查端点 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..18449b1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ankao + +go 1.25.1 diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go new file mode 100644 index 0000000..e78c370 --- /dev/null +++ b/internal/handlers/handlers.go @@ -0,0 +1,26 @@ +package handlers + +import ( + "encoding/json" + "net/http" +) + +// HomeHandler 首页处理器 +func HomeHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + response := map[string]string{ + "message": "欢迎使用AnKao Web服务", + "version": "1.0.0", + } + json.NewEncoder(w).Encode(response) +} + +// HealthCheckHandler 健康检查处理器 +func HealthCheckHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + response := map[string]string{ + "status": "healthy", + } + json.NewEncoder(w).Encode(response) +} diff --git a/internal/middleware/logger.go b/internal/middleware/logger.go new file mode 100644 index 0000000..6a784ec --- /dev/null +++ b/internal/middleware/logger.go @@ -0,0 +1,26 @@ +package middleware + +import ( + "log" + "net/http" + "time" +) + +// Logger 日志中间件 +func Logger(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + // 调用下一个处理器 + next.ServeHTTP(w, r) + + // 记录请求信息 + log.Printf( + "%s %s %s %v", + r.Method, + r.RequestURI, + r.RemoteAddr, + time.Since(start), + ) + }) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..a5f62a9 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "log" + "net/http" + + "ankao/internal/handlers" + "ankao/internal/middleware" +) + +func main() { + // 创建路由 + mux := http.NewServeMux() + + // 注册路由 + mux.HandleFunc("/", handlers.HomeHandler) + mux.HandleFunc("/api/health", handlers.HealthCheckHandler) + + // 应用中间件 + handler := middleware.Logger(mux) + + // 启动服务器 + port := ":8080" + log.Printf("服务器启动在端口 %s", port) + if err := http.ListenAndServe(port, handler); err != nil { + log.Fatal("服务器启动失败:", err) + } +}