go-chi/chi
go-chi/chi is
a lightweight, idiomatic and composable router for building Go HTTP services.
Integrations
- Chain
httpin.NewInput
middleware withchi.With
method - Enable
path
Directive by usingchi.URLParam
Method
Run Demo
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/ggicci/httpin"
httpin_integration "github.com/ggicci/httpin/integration"
"github.com/go-chi/chi/v5"
)
type ListUserReposInput struct {
Username string `in:"path=username"`
Visibility string `in:"query=visibility"`
Fork bool `in:"query=fork"`
}
func ListUserRepos(rw http.ResponseWriter, r *http.Request) {
// Retrieve you data in one line of code!
input := r.Context().Value(httpin.Input).(*ListUserReposInput)
fmt.Printf("input: %#v\n", input)
}
func init() {
// Register a directive named "path" to retrieve values from `chi.URLParam`,
// i.e. decode path variables.
httpin_integration.UseGochiURLParam("path", chi.URLParam)
}
func main() {
router := chi.NewRouter()
// Bind input struct with handler.
router.With(
httpin.NewInput(ListUserReposInput{}),
).Get("/users/{username}/repos", ListUserRepos)
r, _ := http.NewRequest("GET", "/users/ggicci/repos?visibility=public&fork=1", nil)
rw := httptest.NewRecorder()
router.ServeHTTP(rw, r)
}