* URL Pattern (required) e.g.
"/"
"/:org_shortname/product_image/:style/:filename"
"/:org_shortname/products/(*id).json"
* Function to handle request (required)
* Accepted HTTP verbs (optional, defaults ot all)
* Name (optional) - used to do "reverse routing" i.e. produce a URL from parameters
* Contraints (optional) - further refine the pattern matching e.g. type/format of params
Here's an example from Rails:
Foo::Application.routes.draw do
match "/:org_shortname/product_image/:style/:filename" => 'product_images#product_image',
:as => :product_image, :via => :get, :constraints => { :filename => /.jpg$/ }
# URL pattern: "/:org_shortname/product_image/:style/:filename"
# Function: ProductImagesController.product_image
# Name: :product_image
# Verbs: GET
# Contraints: filename param must end in .jpg
match '/products/(*id).json' => 'products#show', :via => :get
# URL pattern: '/products/(*id).json'
# (*id) globs everything between /products/ and .json including / chars
# Function: ProductsController.show
# Verbs: GET
end
There are many other aspects (much of which is just syntax sugar) of routing in Rails that I'm ignoring initially.
I'm thinking of something like the following:
(routes
("/" home-page)
("/:org_shortname/product_image/:style/:filename" product-image #:verbs (get)
#:name prod-image #:when (regexp-match #rx".jpg$" filename))
("/products/(*id).json" product-show #:verbs (get)))
The first two arguments (url-pattern function) are required, so they're positional. The optional arguments are specified with keywords.
Instead of: #:verbs (get) would it be better to have #:verbs get and dynamically check for an atom vs. list ?
My initial goal is just for the "base" syntax. Sugar can be added later for some cases.
Any feedback is appreciated.
Thanks,
Brian
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
They both have a similar role, but I think there are significant differences (unless I'm misunderstanding):
* It's not uncommon to rearrange the components of a URL pattern. Without name binding, you would need to change both the pattern and the handler function.
* Typing is optional for mine
* I didn't see a way to define further constraints (besides the type of an argument) i.e. ensuring an argument matches a pattern
I thought about not providing the #:name parameter, and that might be possible. Rails provides name_url() and name)_path() functions for absolute and relative url creation - I'll have to weigh the pros/cons of using the name of the handler function as the prefix for those.
> I'm open to other ideas, but :variable is used in a number of routing string patterns in other languages/frameworks and seems fairly standard. My point was being standard with other racket work not other web framework. > I'm not sure I understand how *limiting* the constraint to be a regex is faster than *allowing* the constraint to be a regex i.e. in either case, the URL needs to be parsed and then a function called > to compare a variable to a regex pattern. Am I misunderstanding?The point is to make the routing use something like https://swtch.com/~rsc/regexp/regexp1.html to match a route, and thus can be done only if placeholder are regexp and not an arbitrary racket expression. (That is where the #:when comes if you can't express you constraints with regexp you could use a guard).