Comments (8)
@nhooyr this is how we can keep routers completely independent, and when mounted, we have a middleware that changing the routing path at runtime for what the subrouter expects. Another approach would be to have a single large tree, but there are more downsides to that approach (I wrote a version like that too and scrapped it).
is there something you're running into where the current implementation is problematic?
from chi.
No problems, just trying to understand the implementation. I still see no benefit to making the wildcard parameter not always begin with "/"
.
Also, what are the downsides with a single large tree?
from chi.
@nhooyr whats happening in https://github.com/pressly/chi/blob/master/mux.go#L229-L233 is that after a call to r.Mount("/articles", ArticlesRouter)
and ArticlesRouter is an http.Handler (say a chi.Router specifically), the r
router actually matches on /articles*
pattern and the Mount() function will set the endpoint handler for the /articles*
pattern to a handlerfunc that modifies the routing Path and called the subrouter ArticlesRouter
as you can see in https://github.com/pressly/chi/blob/master/mux.go#L232
then, the main http.Handler of the router method, routeHTTP
, we look at the context RoutePath
or r.URL.Path
for the path to search for a matching handler: https://github.com/pressly/chi/blob/master/mux.go#L330-L333 and https://github.com/pressly/chi/blob/master/mux.go#L343-L347
a large tree didn't work because it would require cloning the nodes from one instance to another, and given its a radix tree with different types of branches at any level, it broke subrouting semantics at certain levels. As I was writing it in a TDD approach, I found certain cases were more complex to solve versus attaching subrouters as implemented in the release.
btw, the "/" is there just because that is what r.URL.Path returns always as the first character, so we keep it consistent when routing as a subrouter. It's a recursive algorithm.
from chi.
Yea, I know why it is there and how it works. I'll clarify my question. Why make the router prepend "/"
in https://github.com/pressly/chi/blob/master/mux.go#L231 . Why not make it prepend "/"
over here https://github.com/pressly/chi/blob/master/tree.go#L367 ? So that line becomes
rctx.URLParams.Add("*", "/" + xsearch)
I see no reason why someone would want a catch all parameter without a slash at the front.
from chi.
because that doesn't make sense, the tree is independent of http routing, and the mux uses the tree to keep track of its patterns with associated handlers, but the mux does the routing and serving handlers
from chi.
hope that clears it up.
from chi.
I'm still not getting it :(
I'll make an example.
package main
import (
"net/http"
"github.com/pressly/chi"
)
func main() {
r := chi.NewRouter()
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(chi.URLParam(r, "*")))
})
http.ListenAndServe(":8080", r)
}
If you run the code and go to localhost:8080/anypath
you will see anypath
. My question is why is it anypath
and not /anypath
?
What benefit is there to not having the slash in front of the wildcard parameter?
from chi.
HTTPRouter adds that slash to make it an absolute path too. See julienschmidt/httprouter#116
I can see why adding it would be a bit confusing though because it's not obvious from the pattern that it would be absolute. I guess that's a good enough reason against it. What do you think?
from chi.
Related Issues (20)
- Panic if rctx is nil HOT 1
- runtime: goroutine stack exceeds 1000000000-byte limit while serving HTTP HOT 2
- Undefined Route Request Leads to Infinite Loop in route matching HOT 6
- How to test if a request path matches the pattern using chi router HOT 1
- chi.RouteContext(r.Context()).RoutePattern() returns ""
- Mount isn't compositing the route patterns HOT 1
- Unexpected Group behaviour HOT 1
- Can we log panics in one line? HOT 1
- log/slog's LogAttrs (and other methods) now requires a context as their first argument HOT 3
- Brotli compression not working
- Feature: Expose Context.methodsAllowed for use by MethodNotAllowedHandler
- Feature: on Go 1.22, fill `(*http.Request).PathValue` with URLParam data HOT 5
- Incorrect Route Matching Comment in chi.go
- [Doubt] Do you support i18n? HOT 4
- Possible bug can lead to security issue HOT 4
- URL params are not available inside middleware on the root router HOT 3
- Is jwtauth to validate jwt ? or can I create new jwt tokens using jwtauth? HOT 1
- Feature: on Go 1.22 pattern can start with an optional HTTP Method HOT 5
- bug: HandlerFunc does not support HTTP methods in patterns like #897
- Huma not respecting API Gateway path? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chi.