Typed route handlers
The request body is decoded from JSON before your handler runs and the response body is encoded when it returns. Invalid input is rejected before it reaches your code.
Koa style middleware and typed route handlers, on top of net/http.
Vox is a small web framework built on two ideas. Requests flow through a chain of middleware, as in Koa, and route handlers declare the types of their request and response bodies in their signature.
package main
import (
"fmt"
"time"
"github.com/aisk/vox"
)
type CreateUser struct {
Name string `json:"name"`
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
app := vox.New()
// A middleware that adds an X-Response-Time header.
app.Use(func(ctx *vox.Context, req *vox.BaseRequest, res *vox.BaseResponse) {
start := time.Now()
ctx.Next()
res.Header.Set("X-Response-Time", fmt.Sprintf("%s", time.Since(start)))
})
// A route with a path parameter and a plain text response.
app.Get("/hello/{name}", func(ctx *vox.Context, req *vox.Request[vox.NoBody], res *vox.Response[string]) {
res.Body = "Hello, " + req.Params["name"] + "!"
})
// A route that decodes a JSON body and responds with JSON.
app.Post("/users", func(ctx *vox.Context, req *vox.Request[CreateUser], res *vox.Response[User]) {
res.Status = 201
res.Body = User{ID: 1, Name: req.Body.Name}
})
app.Run("localhost:3000")
}Vox requires Go 1.27 or later.
| If you want to | Read |
|---|---|
| Build and run a first application | Getting Started |
| Understand how requests are matched | Routing |
| Understand typed handlers | Route Handlers |
| Write your own middleware | Middleware |
| Serve files or expose profiling endpoints | Bundled Middleware |
| Copy a working snippet for a common task | Recipes |
| Look up a type or method | API reference |