Files
v-hono/examples/route_grouping/example.v
T
meiseayoung 3b8d21dd0d fix: update import path to VPM package name (meiseayoung.hono)
- Change all `import hono` to `import meiseayoung.hono` in code and docs
- Ensure compatibility with VPM installation via `v install meiseayoung.hono`
2026-01-02 22:05:12 +08:00

42 lines
1.1 KiB
V

// Route grouping example for v-hono web framework
module main
import net.http
import meiseayoung.hono
fn main() {
mut app := hono.Hono.new()
// Create API sub-application
mut api := hono.Hono.new()
api.get('/users', fn (mut c hono.Context) http.Response {
return c.json('[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]')
})
api.get('/users/:id', fn (mut c hono.Context) http.Response {
user_id := c.params['id'] or { 'unknown' }
return c.json('{"id": ${user_id}, "name": "User ${user_id}"}')
})
api.post('/users', fn (mut c hono.Context) http.Response {
return c.json('{"message": "User created"}')
})
// Mount API routes under /api prefix
app.route('/api', mut api)
// Root route
app.get('/', fn (mut c hono.Context) http.Response {
return c.html('<h1>Welcome to v-hono!</h1><p>API available at /api</p>')
})
println('Server starting on http://127.0.0.1:3000')
println('Routes:')
println(' GET / - Welcome page')
println(' GET /api/users - List users')
println(' GET /api/users/:id - Get user by ID')
println(' POST /api/users - Create user')
app.listen(':3000')
}