v0.3.4
v-hono
A high-performance V language web framework inspired by Hono.js.
Features
- 🚀 High Performance - Hybrid routing with LRU cache for optimal speed
- 🎯 Simple API - Clean and intuitive API design inspired by Hono.js
- 🔧 Middleware Support - Flexible middleware system with onion model
- 📁 Static File Serving - Built-in static file server with caching
- 🔐 Security - Path validation and security utilities
- 📤 File Upload - Chunked file upload support
- 🗄️ Database - SQLite integration for data persistence
- 🔑 Authentication - Built-in auth system with JWT-like tokens
Installation
From VPM
v install meiseayoung.hono
From GitHub
v install --git https://github.com/meiseayoung/v-hono
Local Development
Clone the repository and the module will be available for import:
git clone https://github.com/meiseayoung/v-hono.git
cd v-hono
Quick Start
import net.http
import hono
fn main() {
mut app := hono.Hono.new()
app.get('/', fn (mut c hono.Context) http.Response {
return c.text('Hello, World!')
})
app.get('/json', fn (mut c hono.Context) http.Response {
return c.json('{"message": "Hello, JSON!"}')
})
app.get('/users/:id', fn (mut c hono.Context) http.Response {
user_id := c.params['id'] or { 'unknown' }
return c.json('{"user_id": "${user_id}"}')
})
// Redirect example
app.get('/old-page', fn (mut c hono.Context) http.Response {
return c.redirect('/new-page', 301)
})
app.listen(':3000')
}
Middleware
import net.http
import time
import hono
fn main() {
mut app := hono.Hono.new()
// Logger middleware
app.use(fn (mut c hono.Context, next fn (mut hono.Context) http.Response) http.Response {
start := time.now()
response := next(mut c)
duration := time.since(start)
println('[${c.req.method}] ${c.path} - ${duration}')
return response
})
app.get('/', fn (mut c hono.Context) http.Response {
return c.text('Hello with middleware!')
})
app.listen(':3000')
}
Route Grouping
import net.http
import 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"}]')
})
api.get('/users/:id', fn (mut c hono.Context) http.Response {
user_id := c.params['id'] or { 'unknown' }
return c.json('{"id": ${user_id}}')
})
// Mount API routes under /api prefix
app.route('/api', mut api)
app.listen(':3000')
}
Static File Serving
import net.http
import hono
fn main() {
mut app := hono.Hono.new()
// Serve static files from ./public directory
app.use(hono.serve_static(hono.StaticOptions{
root: './public'
path: '/static'
}))
app.listen(':3000')
}
API Reference
Hono
Hono.new()- Create a new Hono applicationapp.get(path, handler)- Register GET routeapp.post(path, handler)- Register POST routeapp.put(path, handler)- Register PUT routeapp.delete(path, handler)- Register DELETE routeapp.patch(path, handler)- Register PATCH routeapp.all(path, handler)- Register route for all methodsapp.use(middleware)- Add middlewareapp.route(prefix, subapp)- Mount sub-applicationapp.listen(port)- Start server
Context
c.text(data)- Return text responsec.json(data)- Return JSON responsec.html(data)- Return HTML responsec.file(path)- Return file responsec.redirect(url, status_code...)- Redirect to URL (default status: 302)c.status(code)- Set response status codec.params- Route parametersc.query- Query parametersc.body- Request bodyc.headers- Response headers
Project Structure
v-hono/
├── app.v # Main application and routing
├── router.v # Hybrid router implementation
├── fast_router.v # Fast router with precompiled regex
├── trie_router.v # Trie-based router
├── cache.v # LRU cache implementation
├── request.v # Request context
├── response.v # Response utilities
├── static.v # Static file serving
├── security.v # Security utilities
├── config.v # Configuration management
├── logger.v # Logging system
├── multipart.v # Multipart form parsing
├── upload.v # File upload handling
├── database.v # Database integration
├── auth.v # Authentication system
├── auth_routes.v # Auth route handlers
├── error_handler.v # Error handling
├── v.mod # Module definition
├── README.md # Documentation
├── examples/ # Example applications
├── tests/ # Test files
└── docs/ # Additional documentation
Examples
See the examples/ directory for more examples:
examples/basic/- Basic usageexamples/middleware/- Middleware usageexamples/route_grouping/- Route groupingexamples/redirect_demo.v- Redirect functionality examples
License
MIT License
Languages
V
84.1%
C
10%
HTML
3.6%
Go
1.9%
Shell
0.2%