完成
This commit is contained in:
@@ -1,38 +1,165 @@
|
||||
# Web 服务器项目
|
||||
|
||||
一个简单的 HTTP Web 服务器,提供 RESTful API。
|
||||
这是一个简单的 HTTP Web 服务器项目,演示了 Go 语言在网络编程、并发处理和 RESTful API 开发方面的应用。
|
||||
|
||||
## 功能特性
|
||||
- HTTP 服务器
|
||||
- RESTful API 端点
|
||||
## 项目特性
|
||||
|
||||
- HTTP 服务器基础功能
|
||||
- RESTful API 设计
|
||||
- JSON 数据处理
|
||||
- 路由处理
|
||||
- 路由管理
|
||||
- 中间件支持
|
||||
- 静态文件服务
|
||||
- 并发请求处理
|
||||
- 错误处理和日志记录
|
||||
- 简单的用户管理系统
|
||||
|
||||
## API 端点
|
||||
- `GET /` - 首页
|
||||
- `GET /api/users` - 获取用户列表
|
||||
- `POST /api/users` - 创建新用户
|
||||
- `GET /api/users/{id}` - 获取特定用户
|
||||
- `PUT /api/users/{id}` - 更新用户
|
||||
- `DELETE /api/users/{id}` - 删除用户
|
||||
## 项目结构
|
||||
|
||||
## 运行方法
|
||||
```bash
|
||||
cd 03-web-server
|
||||
go run main.go
|
||||
```
|
||||
03-web-server/
|
||||
├── README.md # 项目说明文档
|
||||
├── main.go # 主程序入口
|
||||
├── server/ # 服务器核心包
|
||||
│ ├── server.go # HTTP 服务器
|
||||
│ ├── router.go # 路由管理
|
||||
│ ├── middleware.go # 中间件
|
||||
│ └── handlers.go # 请求处理器
|
||||
├── models/ # 数据模型
|
||||
│ └── user.go # 用户模型
|
||||
├── static/ # 静态文件
|
||||
│ ├── index.html # 首页
|
||||
│ ├── style.css # 样式文件
|
||||
│ └── script.js # JavaScript 文件
|
||||
├── data/ # 数据文件
|
||||
│ └── users.json # 用户数据
|
||||
└── server_test.go # 测试文件
|
||||
```
|
||||
|
||||
服务器将在 http://localhost:8080 启动
|
||||
## 运行方法
|
||||
|
||||
```bash
|
||||
# 进入项目目录
|
||||
cd 10-projects/03-web-server
|
||||
|
||||
# 运行程序
|
||||
go run main.go
|
||||
|
||||
# 或者编译后运行
|
||||
go build -o webserver main.go
|
||||
./webserver
|
||||
```
|
||||
|
||||
## API 接口
|
||||
|
||||
### 用户管理 API
|
||||
|
||||
- `GET /api/users` - 获取所有用户
|
||||
- `GET /api/users/{id}` - 获取指定用户
|
||||
- `POST /api/users` - 创建新用户
|
||||
- `PUT /api/users/{id}` - 更新用户信息
|
||||
- `DELETE /api/users/{id}` - 删除用户
|
||||
|
||||
### 其他接口
|
||||
|
||||
- `GET /` - 首页
|
||||
- `GET /health` - 健康检查
|
||||
- `GET /api/stats` - 服务器统计信息
|
||||
- `GET /static/*` - 静态文件服务
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 启动服务器
|
||||
|
||||
```bash
|
||||
# 获取用户列表
|
||||
$ go run main.go
|
||||
🚀 服务器启动成功
|
||||
📍 地址: http://localhost:8080
|
||||
📊 健康检查: http://localhost:8080/health
|
||||
📚 API文档: http://localhost:8080/api
|
||||
```
|
||||
|
||||
### API 调用示例
|
||||
|
||||
```bash
|
||||
# 获取所有用户
|
||||
curl http://localhost:8080/api/users
|
||||
|
||||
# 创建新用户
|
||||
curl -X POST http://localhost:8080/api/users \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name":"张三","email":"zhangsan@example.com"}'
|
||||
```
|
||||
-d '{"name":"张三","email":"zhangsan@example.com","age":25}'
|
||||
|
||||
# 获取指定用户
|
||||
curl http://localhost:8080/api/users/1
|
||||
|
||||
# 更新用户信息
|
||||
curl -X PUT http://localhost:8080/api/users/1 \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name":"张三","email":"zhangsan@gmail.com","age":26}'
|
||||
|
||||
# 删除用户
|
||||
curl -X DELETE http://localhost:8080/api/users/1
|
||||
|
||||
# 健康检查
|
||||
curl http://localhost:8080/health
|
||||
|
||||
# 服务器统计
|
||||
curl http://localhost:8080/api/stats
|
||||
```
|
||||
|
||||
### 响应示例
|
||||
|
||||
```json
|
||||
// GET /api/users
|
||||
{
|
||||
"status": "success",
|
||||
"data": [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "张三",
|
||||
"email": "zhangsan@example.com",
|
||||
"age": 25,
|
||||
"created_at": "2024-01-01T10:00:00Z",
|
||||
"updated_at": "2024-01-01T10:00:00Z"
|
||||
}
|
||||
],
|
||||
"count": 1
|
||||
}
|
||||
|
||||
// GET /health
|
||||
{
|
||||
"status": "healthy",
|
||||
"timestamp": "2024-01-01T10:00:00Z",
|
||||
"uptime": "1h30m45s",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
```
|
||||
|
||||
## 学习要点
|
||||
|
||||
这个项目综合运用了以下 Go 语言特性:
|
||||
|
||||
1. **HTTP 服务器**: 使用 `net/http` 包创建 Web 服务器
|
||||
2. **路由管理**: 实现 RESTful 路由和参数解析
|
||||
3. **JSON 处理**: 请求和响应的 JSON 序列化/反序列化
|
||||
4. **中间件模式**: 日志记录、CORS、认证等中间件
|
||||
5. **并发处理**: 利用 goroutine 处理并发请求
|
||||
6. **错误处理**: HTTP 错误响应和日志记录
|
||||
7. **文件操作**: 静态文件服务和数据持久化
|
||||
8. **结构体和接口**: 数据模型和服务接口设计
|
||||
9. **包管理**: 多包项目结构和依赖管理
|
||||
10. **测试**: HTTP 服务器和 API 的测试
|
||||
|
||||
## 扩展建议
|
||||
|
||||
1. 添加用户认证和授权(JWT)
|
||||
2. 实现数据库集成(MySQL、PostgreSQL)
|
||||
3. 添加缓存支持(Redis)
|
||||
4. 实现 WebSocket 支持
|
||||
5. 添加 API 限流和熔断
|
||||
6. 集成 Swagger API 文档
|
||||
7. 添加配置文件支持
|
||||
8. 实现优雅关闭
|
||||
9. 添加监控和指标收集
|
||||
10. 支持 HTTPS 和 HTTP/2
|
Reference in New Issue
Block a user