Prefix Handler clarification

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Prefix Handler clarification

WeakSauce
Hi Everyone,
I have a question on how the prefix Handler should work.
According to the source docs

 public static Handler prefix(String prefix, Handler handler) ...

// Creates a handler that delegates to the given handlers if the request path starts with the given prefix

I have the following bare bones Ratpack.groovy file

ratpack {
  handlers {
        prefix("foo"){
         get{
            render groovyTemplate("index.html", title: "My Ratpack Fooy App")
         }
        }
    assets "public"
  }
}

It works if I go to http://localhost:5050/foo , but doesn't work if I go to http://localhost:5050/fooBar. Based on the statement, I would have thought that because the path started with foo, that this would have matched, or that http://localhost:5050/foo/bar would have worked. If that is not the intended purpose, no problem. Just wanting to make sure that I am not doing something incorrectly.


Thanks,
Juan Vazquez
@javazquez

Reply | Threaded
Open this post in threaded view
|

Re: Prefix Handler clarification

Luke Daley
Administrator
The get {} handler binds strictly to the path (that is, path “” appending to the parent path binding which in this case is “foo”). If you use handler {} (which does no path binding) instead of get {} you’ll see that it is called.

You can experiment by looking at the state of the path binding in the debugger…

prefix(“foo”) {
  handler {
    def pathBinding = get(ratpack.path.PathBinding)
  }  
}
Reply | Threaded
Open this post in threaded view
|

Re: Prefix Handler clarification

WeakSauce
Thanks Luke,

I was able to get http://localhost:5050/foo/bar/ to work using your suggestion. I can't seem to get it to work with http://localhost:5050/fooy. I don't understanding what prefix means in terms of a path and how matches work. Are matches only happening against the tokens that are split by '/' in the path
ie.
/foo/bar/ -> [foo, bar]  matches because prefix exactly matches 'foo' in  prefix('foo')

where as

/fooy -> [fooy]  fails as it doesn't directly match String 'foo' in  prefix('foo')

When I hear matches, I immediately think Regex and that is what is really driving my question on how this particular function works.

Sorry if I am being a difficult user.

Thanks
Juan
Reply | Threaded
Open this post in threaded view
|

Re: Prefix Handler clarification

Luke Daley
Administrator
 
Hi Jaun,

It’s never a problem to ask questions.  

Yes, the “/” is significant. The DSL methods are geared towards the common cases,. and wanting to “match” on path components is the most comment.  

Our docs for more complex cases are woefully inadequate, but I can point you at the code.  

https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/path/PathBinders.java 
https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/path/PathBinder.java
https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/handling/Handlers.java#L262-262

Basically, a PathBinder decides whether or not the path “matches”.
`PathBinders` gives you some ways to create pattern based implementations.
Handlers.path() gives you a handler that delegates to another handler if the current path “matches” (as determined by the PathBinder).
rus
Reply | Threaded
Open this post in threaded view
|

Re: Prefix Handler clarification

rus

The tests here might help too https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/test/groovy/ratpack/path/PathRoutingSpec.groovy

On 4 Aug 2014 05:12, "Luke Daley [via Ratpack Forum]" <[hidden email]> wrote:
 
Hi Jaun,

It’s never a problem to ask questions.  

Yes, the “/” is significant. The DSL methods are geared towards the common cases,. and wanting to “match” on path components is the most comment.  

Our docs for more complex cases are woefully inadequate, but I can point you at the code.  

https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/path/PathBinders.java 
https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/path/PathBinder.java
https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/handling/Handlers.java#L262-262

Basically, a PathBinder decides whether or not the path “matches”.
`PathBinders` gives you some ways to create pattern based implementations.
Handlers.path() gives you a handler that delegates to another handler if the current path “matches” (as determined by the PathBinder).



If you reply to this email, your message will be added to the discussion below:
http://forum.ratpack.io/Prefix-Handler-clarification-tp571p574.html
To start a new topic under Ratpack Forum, email [hidden email]
To unsubscribe from Ratpack Forum, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: Prefix Handler clarification

WeakSauce
In reply to this post by Luke Daley
Luke,

Thank you, this explanation is very helpful and the links to code are just what I needed.

Juan