gorilla/mux 🦍
gorilla/mux is
A powerful HTTP router and URL matcher for building Go web servers
Integrations
- Enable
path
directive by usingmux.Vars
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/gorilla/mux"
"github.com/justinas/alice"
)
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 `mux.Vars`,
// i.e. decode path variables.
httpin_integration.UseGorillaMux("path", mux.Vars)
}
func main() {
router := mux.NewRouter()
// Bind input struct with handler.
router.Handle("/users/{username}/repos", alice.New(
httpin.NewInput(ListUserReposInput{}),
).ThenFunc(ListUserRepos)).Methods("GET")
r, _ := http.NewRequest("GET", "/users/ggicci/repos?visibility=public&fork=1", nil)
rw := httptest.NewRecorder()
router.ServeHTTP(rw, r)
}