From 1daabdc3893bea98415d6d95b631dd5ad4dd2b2c Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 18 Sep 2024 15:11:09 +0200 Subject: [PATCH] Remove middleware.FindPattern for now --- _examples/find-pattern/main.go | 22 ----------- middleware/find_pattern.go | 29 -------------- middleware/find_pattern_test.go | 67 --------------------------------- 3 files changed, 118 deletions(-) delete mode 100644 _examples/find-pattern/main.go delete mode 100644 middleware/find_pattern.go delete mode 100644 middleware/find_pattern_test.go diff --git a/_examples/find-pattern/main.go b/_examples/find-pattern/main.go deleted file mode 100644 index 76a139f5..00000000 --- a/_examples/find-pattern/main.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" -) - -func main() { - r := chi.NewRouter() - r.Use(middleware.FindPattern(r, func(pattern string) { - fmt.Printf("pattern=%s\n", pattern) - })) - - r.Get("/hello/{name}", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(fmt.Sprintf("hello, %s", chi.URLParam(r, "name")))) - }) - - http.ListenAndServe(":3333", r) -} diff --git a/middleware/find_pattern.go b/middleware/find_pattern.go deleted file mode 100644 index 48a0b6b9..00000000 --- a/middleware/find_pattern.go +++ /dev/null @@ -1,29 +0,0 @@ -package middleware - -import ( - "net/http" - - "github.com/go-chi/chi/v5" -) - -// Find the route pattern for the request path. -// -// This middleware does not need to be the last middleware to resolve the -// route pattern. The pattern is fully resolved before the request has been -// handled. -func FindPattern(routes chi.Routes, callback func(pattern string)) func(http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - // Find mutates the context so always make a new one - rctx := chi.NewRouteContext() - path := r.URL.Path - op := r.Method - pattern := routes.Find(rctx, op, path) - callback(pattern) - - next.ServeHTTP(w, r) - } - return http.HandlerFunc(fn) - } - -} diff --git a/middleware/find_pattern_test.go b/middleware/find_pattern_test.go deleted file mode 100644 index e7b5e8d3..00000000 --- a/middleware/find_pattern_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package middleware - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/go-chi/chi/v5" -) - -func TestFindPattern(t *testing.T) { - t.Parallel() - - var tests = []struct { - pattern string - path string - }{ - { - "/", - "/", - }, - { - "/hi", - "/hi", - }, - { - "/{id}", - "/123", - }, - { - "/{id}/hello", - "/123/hello", - }, - { - "/users/*", - "/users/123", - }, - { - "/users/*", - "/users/123/hello", - }, - } - - for _, tt := range tests { - var tt = tt - t.Run(tt.pattern, func(t *testing.T) { - t.Parallel() - - recorder := httptest.NewRecorder() - - r := chi.NewRouter() - r.Use(FindPattern(r, func(pattern string) { - if pattern != tt.pattern { - t.Errorf("actual pattern \"%s\" does not equal expected pattern \"%s\"", pattern, tt.pattern) - } - })) - - r.Get(tt.pattern, func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("")) - }) - - req := httptest.NewRequest("GET", tt.path, nil) - r.ServeHTTP(recorder, req) - recorder.Result() - }) - } -}