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
Build & Run
Standard Build (picoev backend)
v your_app.v -o app.exe
./app.exe
High-Performance Build (uSockets backend)
For maximum performance with uSockets backend:
v -enable-globals -cc gcc -ldflags "-ldbghelp" your_app.v -o app.exe
./app.exe
Note: The -enable-globals flag is required for uSockets backend. The -ldflags "-ldbghelp" fixes Windows linking issues.
Windows Prerequisites
To use uSockets backend on Windows, you need:
-
GCC (MinGW-w64) - C compiler
# Install via Scoop scoop install mingw # Or via Chocolatey choco install mingw # Or download from: https://www.mingw-w64.org/downloads/ -
V Compiler - Version 0.4.x or later
# Install via Scoop scoop install vlang # Or download from: https://vlang.io/ -
Verify Installation
gcc --version # Should show MinGW-w64 GCC v version # Should show V 0.4.x
The uSockets library and libuv are pre-compiled and included in the usockets/lib/ directory.
Using uSockets Backend
import meiseayoung.hono
fn main() {
mut app := hono.Hono.new()
app.get('/', fn (mut c hono.Context) http.Response {
return c.text('Hello, World!')
})
// Use uSockets backend (high concurrency optimized)
app.listen_usockets(3000)
// Or use default picoev backend
// app.listen(':3000')
}
See benchmark/README.md for performance benchmarks and usockets/README.md for uSockets integration details.
High Concurrency Support
v-hono with uSockets backend supports 10,000+ concurrent connections out of the box.
Performance Results
| Concurrent | RPS | Avg Latency | P99 Latency | Success Rate |
|---|---|---|---|---|
| 5,000 | 81,524 | 60.77ms | 295.17ms | 100% |
| 6,000 | 56,290 | 98.46ms | 557.47ms | 100% |
| 7,000 | 42,895 | 130.04ms | 824.80ms | 100% |
| 8,000 | 30,537 | 165.27ms | 1058.05ms | 100% |
| 9,000 | 28,693 | 215.86ms | 1422.67ms | 100% |
| 10,000 | 24,198 | 240.90ms | 1602.10ms | 100% |
System Configuration (macOS)
For optimal high-concurrency performance:
# Increase socket backlog limit
sudo sysctl -w kern.ipc.somaxconn=8192
# Increase file descriptor limit
ulimit -n 65535
See docs/HIGH_CONCURRENCY.md for detailed optimization guide.
Quick Start
import net.http
import meiseayoung.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 meiseayoung.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 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"}]')
})
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 meiseayoung.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