How to test if a request path matches the pattern using chi router
- Dominant language
- Go
- Stars
- 22.8k
- Forks
- 1.2k
- Avg merge
- 5h 17m
- Merged PRs (30d)
- 10
Description
I'm creating a proxy server, and I has this route: `/proxy/*`
When the requests occours, I get this URL param `*`, and I check this param if it matches with my internal records on a database.
For that, I was using a chi Router like this:
```go
func NewRouter(pathPattern, method string) (rt chi.Router, err error) {
rt = chi.NewRouter()
defer func() {
e := recover()
if e != nil {
err = serror.New("%s", e)
rt = nil
}
}()
rt.Method(method, pathPattern, http.HandlerFunc(dummyHandler))
return rt, nil
}
func preparePath(pathPattern string, r *http.Request) (string, bool) {
requestPath := chi.URLParam(r, "*")
rt, e := NewRouter(pathPattern, r.Method)
if e != nil {
return "", false
}
ctx := chi.NewRouteContext()
if !rt.Match(ctx, r.Method, requestPath) {
return "", false
}
return requestPath, true
}
```
My concern about this code is, if I can do it without affecting the actual chi instance used inside this backend/server.
- Note that I'm instantiating a new router, I intercept and recover from a panic in the case of the `pathPattern` is invalid.
- Doing some tests I noticed an random behavior on the actual chi instance, some routes has starting to respond 404, but I wasn't able to reproduce again.
There is another way to use the route match logic without instantiate another chi router?
Contributor guide
Research direction
The issue names NewRouter, preparePath, chi.Router.Match, and chi.URLParam, but no repository files or tests. Start by tracing the router matching API and its route-context handling; done means establishing whether matching can be performed independently of the server router and documenting or demonstrating the supported approach.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100