// V-Hono 路由分组示例 // 参考 Hono.js 的路由分组功能: https://hono.dev/docs/api/routing#grouping // // Hono.js 路由分组示例: // ```typescript // const book = new Hono() // book.get('/', (c) => c.text('List Books')) // book.get('/:id', (c) => c.text('Get Book: ' + c.req.param('id'))) // // const app = new Hono() // app.route('/books', book) // ``` import net.http import hono fn main() { println('🚀 V-Hono 路由分组示例启动中...') // 创建主应用 mut app := hono.Hono.new() // 添加全局日志中间件 app.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response { println('[LOG] ${c.req.method} ${c.path}') return next(mut c) }) // 根路由 app.get('/', fn (mut c hono.Context) http.Response { return c.html(generate_index_page()) }) // ======================================== // 路由分组: Books API - /api/books // ======================================== mut books := hono.Hono.new() // 在子应用中定义相对路径,挂载时会自动添加前缀 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}"}') }) books.put('/:id', fn (mut c hono.Context) http.Response { book_id := c.params['id'] return c.json('{"message": "Book updated", "id": "${book_id}"}') }) books.delete('/:id', fn (mut c hono.Context) http.Response { c.status(204) return c.text('') }) // 挂载 books 路由组到 /api/books app.route('/api/books', mut books) // ======================================== // 路由分组: Users API - /api/users // ======================================== mut users := hono.Hono.new() users.get('/', fn (mut c hono.Context) http.Response { return c.json('[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]') }) users.get('/:id', fn (mut c hono.Context) http.Response { user_id := c.params['id'] return c.json('{"id": "${user_id}", "name": "Alice", "email": "alice@example.com"}') }) users.get('/:user_id/posts', fn (mut c hono.Context) http.Response { user_id := c.params['user_id'] return c.json('[{"id": 1, "user_id": "${user_id}", "title": "My First Post"}]') }) users.get('/:user_id/posts/:post_id', fn (mut c hono.Context) http.Response { user_id := c.params['user_id'] post_id := c.params['post_id'] return c.json('{"id": "${post_id}", "user_id": "${user_id}", "title": "My Post"}') }) users.post('/', fn (mut c hono.Context) http.Response { c.status(201) return c.json('{"message": "User created", "body": "${c.body}"}') }) // 挂载 users 路由组到 /api/users app.route('/api/users', mut users) // ======================================== // 路由分组: Admin API - /admin // ======================================== mut admin := hono.Hono.new() 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"}]') }) admin.get('/settings', fn (mut c hono.Context) http.Response { return c.json('{"theme": "dark", "language": "zh-CN"}') }) // 挂载 admin 路由组到 /admin app.route('/admin', mut admin) // ======================================== // 其他路由(直接在主应用定义) // ======================================== app.get('/health', fn (mut c hono.Context) http.Response { return c.json('{"status": "ok"}') }) app.get('/api/version', fn (mut c hono.Context) http.Response { return c.json('{"version": "1.0.0", "name": "V-Hono API"}') }) // 打印路由信息 print_routes_info() // 启动服务器 app.listen(':8080') } // 生成首页 HTML fn generate_index_page() string { return '
参考 Hono.js 路由分组 实现
// 创建路由组
mut books := hono.Hono.new()
// 在子应用中定义相对路径
books.get("/", handler) // -> /api/books
books.get("/:id", handler) // -> /api/books/:id
books.post("/", handler) // -> /api/books
// 挂载到主应用
app.route("/api/books", mut books)