Giter Site home page Giter Site logo

Comments (8)

pkieltyka avatar pkieltyka commented on May 6, 2024

@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.

nhooyr avatar nhooyr commented on May 6, 2024

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.

pkieltyka avatar pkieltyka commented on May 6, 2024

@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.

nhooyr avatar nhooyr commented on May 6, 2024

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.

pkieltyka avatar pkieltyka commented on May 6, 2024

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.

pkieltyka avatar pkieltyka commented on May 6, 2024

hope that clears it up.

from chi.

nhooyr avatar nhooyr commented on May 6, 2024

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.

nhooyr avatar nhooyr commented on May 6, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.