Files
v-hono/ROUTE_GROUPING_CHANGELOG.md
T

167 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 路由分组功能更新日志
**日期**: 2024-12-29
## 新增功能
### 路由分组 (Route Grouping)
参考 [Hono.js 路由分组](https://hono.dev/docs/api/routing#grouping) 实现了 V-Hono 的路由分组功能。
#### 使用方式
```v
import hono
fn main() {
mut app := hono.Hono.new()
// 创建子应用(路由组)
mut books := hono.Hono.new()
books.get('/', handler) // -> /api/books
books.get('/:id', handler) // -> /api/books/:id
books.post('/', handler) // -> /api/books
books.put('/:id', handler) // -> /api/books/:id
books.delete('/:id', handler) // -> /api/books/:id
// 挂载到主应用
app.route('/api/books', mut books)
app.listen(':8080')
}
```
### 子应用中间件继承 (2024-12-29 新增)
子应用的中间件会自动继承到主应用,并只对该路由前缀下的请求生效。
```v
mut api := hono.Hono.new()
// 这个中间件只对 /api/* 路由生效
api.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response {
c.headers['X-API-Version'] = '1.0'
return next(mut c)
})
api.get('/version', handler)
app.route('/api', mut api)
```
### all() 方法 (2024-12-29 新增)
一次性为所有 HTTP 方法注册同一个处理器。
```v
// 匹配 GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
app.all('/echo', fn (mut c hono.Context) http.Response {
return c.json('{"method": "${c.req.method}"}')
})
```
### notFound() 处理器 (2024-12-29 新增)
自定义 404 响应。
```v
app.not_found(fn (mut c hono.Context) http.Response {
c.status(404)
return c.json('{"error": "Not Found", "path": "${c.path}"}')
})
```
### onError() 处理器 (2024-12-29 新增)
自定义错误响应。
```v
app.on_error(fn (error_msg string, status_code int, mut c hono.Context) http.Response {
c.status(status_code)
return c.json('{"error": "${error_msg}", "code": ${status_code}}')
})
```
## 代码变更
### hono/app.v
1. **重写 `route()` 方法**
- 正确合并子应用路由到 `fast_router`、`context_hybrid_router` 和 `context_trie_router`
- 自动为子应用路由添加前缀路径
- 合并子应用的中间件到 `route_middlewares`
2. **新增 `PrefixedHandler` 结构体**
```v
pub struct PrefixedHandler {
pub:
path string
inner IHandler
}
```
- 实现 `IHandler` 接口
- 用于包装原始 handler 并添加路径前缀
3. **新增 `merge_routes_for_method()` 辅助函数**
- 处理单个 HTTP 方法的路由合并逻辑
- 正确处理根路径 `/` 的情况
- 将路由添加到所有三个路由器
4. **新增 `all()` 方法**
- 一次性为所有 HTTP 方法注册处理器
5. **新增 `not_found()` 方法**
- 设置自定义 404 处理器
6. **新增 `on_error()` 方法**
- 设置自定义错误处理器
7. **新增 `route_middlewares` 字段**
- 存储路由前缀对应的中间件列表
8. **新增 `get_middlewares_for_path()` 方法**
- 获取路径对应的所有中间件(全局 + 路由前缀匹配的)
9. **新增 `exec_context_middlewares_with_list()` 方法**
- 使用指定中间件列表执行洋葱模型
### 新增文件
- `route_grouping_example.v` - 路由分组基础示例
- `route_grouping_example_v2.v` - 路由分组增强示例(包含中间件继承、all()、notFound/onError)
## 测试修复
### test_cache_fix.v
修正了 LRU 缓存测试逻辑:
- 原测试期望 `key1` 被淘汰,但由于 `get('key1')` 会将其移到头部,实际应该淘汰 `key2`
- 这是 LRU 缓存的正确行为
### test_router_optimization.v
更新为使用新 API:
- `hono.new_optimized_router()` → `hono.FastRouter.new()`
- `hono.new_context_hybrid_router()` → `hono.ContextHybridRouter.new()`
- 修复路由匹配返回值处理
- 移除不存在的 `RouteDefinition` 和 `add_routes_batch`
### test_comprehensive_suite.v
更新为使用新 API:
- `hono.new_context_lru_cache()` → `hono.ContextLRUCache.new()`
- 修复 `validate_file_path` 和 `validate_file_hash` 返回类型处理
- 使用 `Context` 方法替代独立的错误处理函数
- 简化文件上传测试为配置验证测试
## 测试结果
所有测试通过:
- `test_simple_suite.v` - 10/10 ✅
- `test_unit_suite.v` - 10/10 ✅
- `test_comprehensive_suite.v` - 10/10 ✅
- `test_router_optimization.v` - ✅
- `test_dynamic_routes_comprehensive.v` - ✅
- `test_final_integration.v` - ✅
- `test_cache_fix.v` - ✅
- `route_grouping_example.v` - ✅