Git Product home page Git Product logo

Comments (11)

elithrar avatar elithrar commented on July 28, 2024

You'll need to initialize the map if it hasn't been done yet. See
https://github.com/zenazn/goji/blob/master/web/middleware/request_id.go#L62-64
for
an example.

On Sunday, June 15, 2014, Phea Duch [email protected] wrote:

I'm trying to access c.URLParams in a middleware function of mine, however
c.URLParams doesn't seem to be initiated. I am either doing something wrong
on my end or maybe URLParams is initiated after all the middleware calls.


Reply to this email directly or view it on GitHub
#32.

from goji.

phea avatar phea commented on July 28, 2024

Does not work, I think I will just move the particular code from middleware to the handler. Probably makes more sense anyways.

from goji.

elithrar avatar elithrar commented on July 28, 2024

Can you post your code?

On Sunday, June 15, 2014, Phea Duch [email protected] wrote:

Does not work, I think I will just move the particular code from
middleware to the handler. Probably makes more sense anyways.


Reply to this email directly or view it on GitHub
#32 (comment).

from goji.

elithrar avatar elithrar commented on July 28, 2024

Ignore what I posted before.

You have to have this in the handler: middleware applies to all routes
under a router or sub router, and there is no guarantee that the map key
(i.e. c.URLParams["id"]) is unique across that router, as URL params are
effectively scoped to a single route.

/things/show/:id and /user/:id would share the same key from a middleware
perspective, which is not what most package users expect.

You can therefore put it into the handler or use r.URL.Path to get the
whole URL path and use that as you wish.

On Sunday, June 15, 2014, Phea Duch [email protected] wrote:

Does not work, I think I will just move the particular code from
middleware to the handler. Probably makes more sense anyways.


Reply to this email directly or view it on GitHub
#32 (comment).

from goji.

phea avatar phea commented on July 28, 2024

I see, that makes a lot of sense. I will just move the logic to the handler for better readability. Thanks for the help.

from goji.

zenazn avatar zenazn commented on July 28, 2024

Yeah—middleware can change the URL of the incoming request, so it's impossible to bind URL parameters until after the middleware stack runs.

If you're interested in using URL parameters in middleware specifically, and your URL structure is amenable to it, you could do something like this:

m := web.New()
m.Use(someMiddlewareThatUsesID)
goji.Handle("/post/:id/*", m)
m.Get("/post/:id/comments", commentsHandler)

from goji.

bradrydzewski avatar bradrydzewski commented on July 28, 2024

It would be nice to have a little more control over this behavior.

The reason I ask is that I started down the path of implementing goji for my project: github.com/drone/drone

I was trying to use middleware to verify that a user can access a particular repository. The code looks something like this:

func SetRepo(c *web.C, h http.Handler) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {
        var (
            host  = c.URLParams["host"]
            owner = c.URLParams["owner"]
            name  = c.URLParams["repo"]
            user  = ToUser(c)
        )

        repo, err := datastore.GetRepo(c, host, owner, name)
        ...
        role, err := datastore.GetRole(c, user, repo)
        if err != nil || role.Read != true {
            w.WriteHeader(http.StatusNotFound)
            return
        }
        ...

I was trying to troubleshoot why it wasn't working and came across this thread. I don't modify the path in any of my middleware, so I would not be subject the restriction previously mentioned. Either way, could goji track changes to the path and re-parse the parameters in-between middleware?

Just some food for thought ...

from goji.

zenazn avatar zenazn commented on July 28, 2024

@bradrydzewski — It's unfortunately a little trickier than that. The fact that route parsing happens after the middleware stack is pretty deeply baked into Goji's design, and it's not going to be easy to change.

The best motivating example is probably Goji's support for custom URL patterns. Both of the built-in pattern types (Sinatra-style /patterns/:id and regular expressions with captures) only look at the path, but there's no reason someone couldn't write a custom pattern that looked at HTTP headers, for example, or even did something completely unrelated (say, kept a global counter and displayed every 10,000th user a custom page). It's impossible for Goji to keep track of all the variables that might go in to route selection, and therefore it's impossible for Goji to know when to update its cached route selection decision.

I do think this behavior is not at all obvious, and it's especially non-obvious why it must be the case. I certainly owe the world more in-depth documentation here (maybe a blog post?), so thanks for pointing this out.

Come to think of it though, there might be an interesting technical solution here: a special "middleware" called web.Router (or something). If it is placed into the middleware stack explicitly, a routing decision will be made at that point in the stack and the corresponding http.Handler will be placed into an Env variable. At the end of the middleware stack, the Handler will be pulled out of the environment, or if no Handler is set there, routing will be run at that time.

Let me think about this some more, but I think this would allow you to place middleware after the routing logic while still playing nice with the rest of Goji.

from goji.

kurianCoding avatar kurianCoding commented on July 28, 2024

is it possible to set URL PARAMS in middleware?

from goji.

elithrar avatar elithrar commented on July 28, 2024

@kurianCoding You can just save over the map entry or create your own in middleware as you see fit - e.g. c.URLParams["name"] = "Dave" (before calling the next handler)

from goji.

kurianCoding avatar kurianCoding commented on July 28, 2024

@elithrar thanks, I had tried this method. It didn't work. Now its possible to send custom params using the c.Env. I was thinking about using a.URLParams because parameters show on the query string.

from goji.

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.