// V-Hono 路由分组增强示例 // 演示新功能:子应用中间件继承、all() 方法、notFound/onError 处理器 import net.http import hono fn main() { println('🚀 V-Hono 路由分组增强示例启动中...') // 创建主应用 mut app := hono.Hono.new() // ======================================== // 1. 自定义 notFound 处理器 // ======================================== app.not_found(fn (mut c hono.Context) http.Response { c.status(404) return c.json('{"error": "Not Found", "message": "The requested resource does not exist", "path": "${c.path}"}') }) // ======================================== // 2. 自定义 onError 处理器 // ======================================== app.on_error(fn (error_msg string, status_code int, mut c hono.Context) http.Response { c.status(status_code) return c.json('{"error": "Internal Error", "message": "${error_msg}", "code": ${status_code}}') }) // ======================================== // 3. 全局中间件 // ======================================== app.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response { println('[GLOBAL] ${c.req.method} ${c.path}') return next(mut c) }) // 根路由 app.get('/', fn (mut c hono.Context) http.Response { return c.html(generate_index_page()) }) // ======================================== // 4. all() 方法示例 - 匹配所有 HTTP 方法 // ======================================== app.all('/echo', fn (mut c hono.Context) http.Response { return c.json('{"method": "${c.req.method}", "path": "${c.path}", "message": "Echo endpoint handles all HTTP methods"}') }) // ======================================== // 5. 子应用中间件继承示例 - API 路由组 // ======================================== mut api := hono.Hono.new() // API 子应用的中间件(只对 /api/* 路由生效) api.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response { println('[API] Request to API endpoint: ${c.path}') // 添加 API 版本头 c.headers['X-API-Version'] = '1.0' return next(mut c) }) api.get('/version', fn (mut c hono.Context) http.Response { return c.json('{"version": "1.0.0", "name": "V-Hono API"}') }) app.route('/api', mut api) // ======================================== // 6. Books 子应用(带认证中间件) // ======================================== mut books := hono.Hono.new() // Books 子应用的认证中间件 books.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response { println('[BOOKS] Auth check for: ${c.path}') // 模拟认证检查 auth_header := c.req.header.get_custom('Authorization') or { '' } if auth_header == '' { println('[BOOKS] No auth header, allowing public access') } else { println('[BOOKS] Auth header present: ${auth_header}') } return next(mut c) }) books.get('/', fn (mut c hono.Context) http.Response { return c.json('[{"id": 1, "title": "V Programming"}, {"id": 2, "title": "Web Development"}]') }) books.get('/:id', fn (mut c hono.Context) http.Response { book_id := c.params['id'] return c.json('{"id": "${book_id}", "title": "V Programming", "author": "V Team"}') }) books.post('/', fn (mut c hono.Context) http.Response { c.status(201) return c.json('{"message": "Book created", "body": "${c.body}"}') }) // 使用 all() 处理所有方法 books.all('/stats', fn (mut c hono.Context) http.Response { return c.json('{"method": "${c.req.method}", "total_books": 100, "message": "Stats endpoint"}') }) app.route('/api/books', mut books) // ======================================== // 7. Admin 子应用(带严格认证中间件) // ======================================== mut admin := hono.Hono.new() // Admin 子应用的严格认证中间件 admin.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response { println('[ADMIN] Strict auth check for: ${c.path}') auth_header := c.req.header.get_custom('Authorization') or { '' } if !auth_header.starts_with('Bearer admin-') { c.status(401) return c.json('{"error": "Unauthorized", "message": "Admin access required"}') } println('[ADMIN] Admin access granted') return next(mut c) }) admin.get('/', fn (mut c hono.Context) http.Response { return c.json('{"page": "Admin Dashboard", "stats": {"users": 100, "books": 50}}') }) admin.get('/users', fn (mut c hono.Context) http.Response { return c.json('[{"id": 1, "name": "Admin User", "role": "admin"}]') }) app.route('/admin', mut admin) // ======================================== // 8. 健康检查(无中间件) // ======================================== app.get('/health', fn (mut c hono.Context) http.Response { return c.json('{"status": "ok"}') }) // 打印路由信息 print_routes_info() // 启动服务器 app.listen(':8080') } // 生成首页 HTML fn generate_index_page() string { return '
中间件:认证检查(可选)
中间件:严格认证(需要 Bearer admin-* token)
// 1. 自定义 notFound 处理器
app.not_found(fn (mut c hono.Context) http.Response {
c.status(404)
return c.json(\'{"error": "Not Found"}\')
})
// 2. all() 方法 - 匹配所有 HTTP 方法
app.all(\'/echo\', fn (mut c hono.Context) http.Response {
return c.json(\'{"method": "\' + c.req.method + \'"}\')
})
// 3. 子应用中间件继承
mut books := hono.Hono.new()
books.use(auth_middleware) // 只对 /api/books/* 生效
books.get(\'/\', handler)
app.route(\'/api/books\', mut books)
# 测试 all() 方法 curl http://localhost:8080/echo curl -X POST http://localhost:8080/echo curl -X PUT http://localhost:8080/echo # 测试自定义 404 curl http://localhost:8080/not-exist # 测试 Admin 认证中间件 curl http://localhost:8080/admin # 返回 401 curl -H "Authorization: Bearer admin-token" http://localhost:8080/admin # 成功