A pi
is your good helper to build a clean JSON API server using Golang Generics.
type UserData struct {
Name string
Password string
}
var h pi.HandlerFunc = func (ctx pi.Context) error {
data := &UserData{}
err := pi.Format(ctx, data)
if err != nil {
return pi.NewError(400, err.Error())
}
// do sth. actions...
return ctx.Text("hello, world!")
}
sm := pi.NewServerMux(context.Background())
sm.Post("/api/v1/users", h)
http.ListenAndServe("localhost:8080", sm)
go get -u github.com/go-laeo/pi
- Fast routing, routes group, route params and wildcard route
-
net/http
compatible (pi.HandlerFunc
is ahttp.Handler
) -
AutoManually decode HTTP body by usingpi.Format[T any]()
- Middleware supports by using
pi.(ServerMux).Use()
orpi.(HandlerFunc).Connect()
- Built-in
pi.FileServer
for SPA - No third-party depdencies
- Unit tests and benchmarks
See _examples
folder.
Apache 2.0 License