项目初始化
This commit is contained in:
commit
0361f207db
37
.gitignore
vendored
Normal file
37
.gitignore
vendored
Normal file
@ -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/
|
||||||
78
CLAUDE.md
Normal file
78
CLAUDE.md
Normal file
@ -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
|
||||||
62
README.md
Normal file
62
README.md
Normal file
@ -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结构
|
||||||
|
- 健康检查端点
|
||||||
26
internal/handlers/handlers.go
Normal file
26
internal/handlers/handlers.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
26
internal/middleware/logger.go
Normal file
26
internal/middleware/logger.go
Normal file
@ -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),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
28
main.go
Normal file
28
main.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user