Skip to content

Commit

Permalink
Rename find-pattern example
Browse files Browse the repository at this point in the history
  • Loading branch information
jriddle committed Nov 7, 2023
1 parent b1ed5b4 commit 5cf9bc3
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
r := chi.NewRouter()
r.Use(ApiMiddleware(r))
r.Use(FindPatternMiddleware(r))

r.Get("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("hello, %s", chi.URLParam(r, "name"))))
Expand All @@ -18,16 +18,16 @@ func main() {
http.ListenAndServe(":3333", r)
}

// Middleware that prints the API pattern before the request is handled
func ApiMiddleware(router chi.Router) func(http.Handler) http.Handler {
// Middleware that prints the route pattern before the request is handled
func FindPatternMiddleware(router chi.Router) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rctx := chi.NewRouteContext()
path := r.URL.Path
op := r.Method
api := router.Find(rctx, op, path)
pattern := router.Find(rctx, op, path)

fmt.Printf("api=%s\n", api)
fmt.Printf("pattern=%s\n", pattern)

next.ServeHTTP(w, r)
})
Expand Down

0 comments on commit 5cf9bc3

Please sign in to comment.