You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Chris Anderson <jc...@apache.org> on 2009/05/06 22:15:04 UTC

Re: Webmachine dispatcher example

Including the CouchDB dev list here, as there's so much valuable information.

The topic is how to do sane rewriting of URLs under some design doc
path, so we can have flexible URLs for CouchDB, without breaking the
normal API contract we want to hold, as a database.

The goal is that a URL like

/db/_design/myapp/_rewrite/api/products/toasters

could be processed internally as if it were a request to

/db/_design/myapp/_list/products-index/all-products?prefix="toasters"&group_level=1&format=xml

Then you could use a (non-CouchDB) proxy to chop the beginning parts
off the path, so clients would only see:

/api/products/toasters

This is mostly helpful for supporting legacy applications with a
pure-CouchDB backend, but it's a nice bit of flexibility to offer
users in general.


On Wed, May 6, 2009 at 12:54 PM, Justin Sheehy <ju...@iago.org> wrote:
> Hi Chris and Jason,
>
> On Tue, May 5, 2009 at 10:52 PM, Chris Anderson <jc...@apache.org> wrote:
>
>> Jason's been looking into how to build an internal rewriter for
>> CouchDB, so that applications can control their URLs. After your
>> Webmachine talk it got obvious to me that you've probably got code
>> that'd fit right into what we need.
>
> Sounds like a good possible fit indeed.  :-)
>
> At the end of this email is an example almost directly cut-and-pasted
> from an existing client application's dispatch configuration.  One of
> the important tricks you might notice there is the use of pattern-
> matching to decide not just which resource to dispatch to, but also
> what arguments to pass to that resource's init/1 function.
>
> The key to using this (outside webmachine itself) is that the
> dispatcher, given a list of dispatch terms and a request path,
> either produces a term indicating no match or else a 6-tuple:
>
> {Mod, ModOpts, PathTokens, Bindings, AppRoot, StringPath}
>
> Mod here is the atom from the 2nd element of the matching term.
>
> ModOpts is the 3rd element, used in webmachine as args to init/1.
>
> PathTokens and StringPath are the same thing in different forms,
> the path portion matched by '*' either split on "/" or left intact.
>
> Bindings is the dict of bindings indicated by atom terms in the spec,
> bound to the matching terms in the request path.
>
> AppRoot is rarely interesting and useful only for some legacy apps,
> and is generally something like "../.."; it could be deprecated and
> I suspect no one would notice.
>
> WIth this you could easily write out very different patterns which
> dispatched to the same resource, but provided it with different
> inputs.  That sounds like basically what you're looking for.
>
> An admitted downside to the current dispatch code in webmachine
> is that it is a bit more tied in to webmachine's structure than it really
> needs to be -- for purely historical reasons from webmachine's
> original non-public application.  I could happily be convinced to
> rework webmachine_dispatcher just a little bit so that it still supports
> existing webmachine installations but is also useful as a standalone
> module in other applications.
>
> I suspect that with that, and a little work in couch, you could use our
> dispatcher either for all of your URLs or for just ones under your _rewrite
> space.  I'm happy to help you get there; it shouldn't be hard.
>
> I suspect that the hardest part may be deciding exactly what you want
> to expose in terms of rewriting capability via the HTTP/JSON path.
>
>> So any examples of a relatively complex set of dispatch terms would be
>> helpful in this endeavor. Good work on Webmachine! If we weren't set
>> on letting untrusted users write applications (hence the JavaScript)
>> we'd be all over it.
>
> Here's the example you asked for.  I can happily add more details or
> explanation if you want.  Real applications on top of webmachine are
> nearly always a combination of Erlang and Javascript, by the way.
>
> Responding to you made me realize that I really do want to make the
> dispatcher more suitable for outside use, so I'll get on that shortly.
> (I'll fit it around Real Work over the next few days)
>
> I'm happy to chat as much more about this as you wish and to help
> develop the rewriting feature from the dispatch point of view.
>
> Cheers,
>
> -Justin
>
>
>
> {["pageconfig", pageid, "role", roleid], page_config_resource, []}.
> {["api","research"], proxy_resource, [{host_env, research_host},{path, "/r"}]}.
> {["api","news"], proxy_resource, [{host_env,rd_host},{path,"/rd/sig_dev"}]}.
> {["api", "gpe", "news"],proxy_resource, [{host_env,gpipe_host},{path,"/gpe"}]}.
> {["devuse", "metrics"],              metrics_resource,         []}.
> {["devuse", "metrics", bcust_key],   metrics_resource,         []}.
> {["api", "d12n", "search"], d12n_resource, [{mode, search}]}.
> {["api", "d12n", "detail"], d12n_resource, [{mode, detail}]}.
> {["api", "coweb", cokey], coweb_resource, []}.
> {["xavier", "list", '*'], xavier_resource, [{xaviertype, list}]}.
> {["favicon.ico"], fs_resource, [{root, "priv/www/images/favicon.ico"}]}.
> {["japi", bucket, key], japi_resource, ["japi", object]}.
> {["japi", bucket], japi_resource, ["japi", container]}.
> {["jaywalker", bucket, key, '*'], jaywalker_resource, []}.
> {['*'],  fs_resource, [{root, "priv/www"}]}.
>

Cool, this looks like it'd actually be really simple to integrate.

We'd essentially have a single module:function, perhaps
rewrite:internal_request or some such, that would take as an argument,
whatever it is that the dispatcher gives it, so from the JSON side,
each matcher could be something like (ignoring edge cases):

{ "match" : ["api", "products", "*"],
  "rewrite" : ["_list", "all-products", "products-api", {"prefix" :
"*", "group_level" : 1, "format" : "xml"}]
}

On the first _rewrite request we could "compile" this JSON down to the
term format the Webmachine dispatcher needs, and then use it for
rewrites until the design doc is changed.

Disclaimer: I've got other fish to fry, so I can't spend much time on
this feature, other than basic sanity-checking and guidance as far as
trying to make it something CouchDB can support without causing
trouble elsewhere in the API.

Chris

-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: Rewriting URLs

Posted by Paul Davis <pa...@gmail.com>.
On Sat, Jul 4, 2009 at 6:16 PM, Chris Anderson<jc...@apache.org> wrote:
> On Sat, Jul 4, 2009 at 3:12 PM, Paul Davis<pa...@gmail.com> wrote:
>> On Sat, Jul 4, 2009 at 6:08 PM, Noah Slater<ns...@apache.org> wrote:
>>> Hey Jason,
>>>
>>> On Sat, Jul 04, 2009 at 10:34:27PM +0100, Jason Davies wrote:
>>>> The basic idea is you add a "rewrites": [...] member to your design doc.
>>>> This will allow rewriting of any URLs with the prefix mydb/
>>>> _design/app/_rewrite.  The "rewrites" member is a list of rewrite rules
>>>> of the form {match: ["foo/bar/<var>"], rewrite:["_view/myview",
>>>> {startkey: ["<var>"], endkey: ["<var>", {}]}]
>>>
>>> This is great!
>>>
>>> Don't let me contribute stop-energy, but I am thinking that if we're going to do
>>> this, we might want to go the full way an implement this from the root URI of
>>> the whole CouchDB server instead.
>>>
>>> Best,
>>>
>>> --
>>> Noah Slater, http://tumbolia.org/nslater
>>>
>>
>> We've discussed where to push the URL rewriting previously. The
>> biggest argument against a DB wide rewrite scheme is that _design/
>> documents could end up stomping on each other pretty easily.
>>
>> For reference in case other people are confused, the rewrite
>> capabilities would be best used by putting a proxy in front of CouchDB
>> that would rewrite all urls to the appropiate CouchDB path.
>>
>> Something like:
>>
>> Rewrite http://blog.mydomain.com/ to
>> http://127.0.0.1:5984/my_blog_db/_design/sofar/_rewrite/
>>
>
> Eventually this could be implemented in CouchDB itself, with a table
> to lookup app-rewrites for various subdomains or ports. I think it's
> important to limit what the design doc alone can do. Putting something
> like a dictionary of app -> ddoc rewrites in config seems like the
> right place to modify CouchDB at such a fundamental level.
>
> Chris
>

That sounds like a good compromise. The biggest issue I remember was
that replicating in a _design doc could start trouncing things, if it
requires an admin to specify a config it'd place rewrite conflicts in
the 'admin needs to know what he's doing' domain which is fine by me.

>
>
> --
> Chris Anderson
> http://jchrisa.net
> http://couch.io
>

Re: Rewriting URLs

Posted by Jason Davies <ja...@jasondavies.com>.
Hi Noah,

On Sat, Jul 04, 2009 at 11:21:04PM +0100, Noah Slater wrote:
> On Sat, Jul 04, 2009 at 03:16:42PM -0700, Chris Anderson wrote:
> > Eventually this could be implemented in CouchDB itself, with a table
> > to lookup app-rewrites for various subdomains or ports. I think it's
> > important to limit what the design doc alone can do. Putting something
> > like a dictionary of app -> ddoc rewrites in config seems like the
> > right place to modify CouchDB at such a fundamental level.
> 
> I didn't mean to suggest that design documents get to control the parent URI
> space, as this could obviously introduce conflicts. I think a nice approach, as
> Chris suggests, would be split into two levels:
> 
>   * Design document level URI rewriting, but no parent URIs
> 
>   * Server level configuration that rewrites arbitrary URIs
> 
> Do we need these both in the same release? Not sure, but it would be nice!

Just for fun, I made handle_rewrite_req so it can be configured as [httpd]
default_handler.  Chris envisaged a dictionary of app -> ddocs rewrites and
clearly this doesn't do this yet, but it works as a proof-of-concept.

Commit: http://github.com/jasondavies/couchdb/commit/f3ee881ccd13207207e13f230a83e5eddf31509a

To use:

[httpd]
default_handler = {couch_httpd_rewrite, handle_rewrite_req, {"test_suite_db", "rewrite_test"}}

Any suggestions for what the actual config would look like for mapping domains
and ports to databases and design docs?

I'm thinking something like:

[couch_httpd_rewrite]
{"jason.mydomain.com", 80, "/"} = {"mydb", "rootapp"}
{"jason.mydomain.com", 80, "/blog"} = {"mydb", "blogapp"}

I think people will probably want some kind of wildcard matching too though.
Anyway, it's early days still, I think at least supporting basic _rewrite is a
big win.

Cheers,
-- 
Jason Davies

www.jasondavies.com

Re: Rewriting URLs

Posted by Noah Slater <ns...@apache.org>.
On Sun, Jul 05, 2009 at 11:22:43AM -0700, Chris Anderson wrote:
> I do like all the power these give us. Maybe it's worth thinking about
> applying all the rewrite magic on a non-5984 port so we don't have to
> worry about not breaking the database. Complicated.

People will always be able to fuck things up if they don't think things through.
I don't think we should be hand-holding our users. The port that CouchDB uses
should be totally unrelated to anything.

Best,

-- 
Noah Slater, http://tumbolia.org/nslater

Re: Rewriting URLs

Posted by Chris Anderson <jc...@apache.org>.
On Sun, Jul 5, 2009 at 8:43 AM, Noah Slater<ns...@apache.org> wrote:
> On Sun, Jul 05, 2009 at 10:25:50AM +0100, Jason Davies wrote:
>> I made handle_rewrite_req so it can be configured as [httpd] default_handler.
>
> Fantastic!
>
>> [couch_httpd_rewrite]
>> {"jason.mydomain.com", 80, "/"} = {"mydb", "rootapp"}
>> {"jason.mydomain.com", 80, "/blog"} = {"mydb", "blogapp"}
>
> Looks good to me!

I think we should keep in mind that we don't want to break the ability
to replicate. Maybe a /db/_replicate channel that we don't break or
something...

I do like all the power these give us. Maybe it's worth thinking about
applying all the rewrite magic on a non-5984 port so we don't have to
worry about not breaking the database. Complicated.


>
>> I think people will probably want some kind of wildcard matching too though.
>
> What's the use case?
>
> Best,
>
> --
> Noah Slater, http://tumbolia.org/nslater
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: Rewriting URLs

Posted by Jason Davies <ja...@jasondavies.com>.
Hi Noah,

On 5 Jul 2009, at 16:43, Noah Slater wrote:
> On Sun, Jul 05, 2009 at 10:25:50AM +0100, Jason Davies wrote:
>> [couch_httpd_rewrite]
>> {"jason.mydomain.com", 80, "/"} = {"mydb", "rootapp"}
>> {"jason.mydomain.com", 80, "/blog"} = {"mydb", "blogapp"}
>
> Looks good to me!
>
>> I think people will probably want some kind of wildcard matching  
>> too though.
>
> What's the use case?

I was thinking of something like mapping <username>.mydomain.com to  
database <username>.  Could be useful in a situation where each user  
has their own database.  We can always add something like this if the  
need arises though.

Cheers,
--
Jason Davies

www.jasondavies.com


Re: Rewriting URLs

Posted by Noah Slater <ns...@apache.org>.
On Sun, Jul 05, 2009 at 10:25:50AM +0100, Jason Davies wrote:
> I made handle_rewrite_req so it can be configured as [httpd] default_handler.

Fantastic!

> [couch_httpd_rewrite]
> {"jason.mydomain.com", 80, "/"} = {"mydb", "rootapp"}
> {"jason.mydomain.com", 80, "/blog"} = {"mydb", "blogapp"}

Looks good to me!

> I think people will probably want some kind of wildcard matching too though.

What's the use case?

Best,

-- 
Noah Slater, http://tumbolia.org/nslater

Re: Rewriting URLs

Posted by Jason Davies <ja...@jasondavies.com>.
Hi Noah,

On 4 Jul 2009, at 23:21, Noah Slater wrote:

> On Sat, Jul 04, 2009 at 03:16:42PM -0700, Chris Anderson wrote:
>> Eventually this could be implemented in CouchDB itself, with a table
>> to lookup app-rewrites for various subdomains or ports. I think it's
>> important to limit what the design doc alone can do. Putting  
>> something
>> like a dictionary of app -> ddoc rewrites in config seems like the
>> right place to modify CouchDB at such a fundamental level.
>
> I didn't mean to suggest that design documents get to control the  
> parent URI
> space, as this could obviously introduce conflicts. I think a nice  
> approach, as
> Chris suggests, would be split into two levels:
>
>  * Design document level URI rewriting, but no parent URIs
>
>  * Server level configuration that rewrites arbitrary URIs
>
> Do we need these both in the same release? Not sure, but it would be  
> nice!

Just for fun, I made handle_rewrite_req so it can be configured as  
[httpd]
default_handler.  Chris envisaged a dictionary of app -> ddocs  
rewrites and
clearly this doesn't do this yet, but it works as a proof-of-concept.

Commit: http://github.com/jasondavies/couchdb/commit/f3ee881ccd13207207e13f230a83e5eddf31509a

To use:

[httpd]
default_handler = {couch_httpd_rewrite, handle_rewrite_req,  
{"test_suite_db", "rewrite_test"}}

Any suggestions for what the actual config would look like for mapping  
domains
and ports to databases and design docs?

I'm thinking something like:

[couch_httpd_rewrite]
{"jason.mydomain.com", 80, "/"} = {"mydb", "rootapp"}
{"jason.mydomain.com", 80, "/blog"} = {"mydb", "blogapp"}

I think people will probably want some kind of wildcard matching too  
though.
Anyway, it's early days still, I think at least supporting basic  
_rewrite is a
big win.

Cheers,
-- 
Jason Davies

www.jasondavies.com

Re: Rewriting URLs

Posted by Noah Slater <ns...@apache.org>.
On Sat, Jul 04, 2009 at 03:16:42PM -0700, Chris Anderson wrote:
> Eventually this could be implemented in CouchDB itself, with a table
> to lookup app-rewrites for various subdomains or ports. I think it's
> important to limit what the design doc alone can do. Putting something
> like a dictionary of app -> ddoc rewrites in config seems like the
> right place to modify CouchDB at such a fundamental level.

I didn't mean to suggest that design documents get to control the parent URI
space, as this could obviously introduce conflicts. I think a nice approach, as
Chris suggests, would be split into two levels:

  * Design document level URI rewriting, but no parent URIs

  * Server level configuration that rewrites arbitrary URIs

Do we need these both in the same release? Not sure, but it would be nice!

Best,

-- 
Noah Slater, http://tumbolia.org/nslater

Re: Rewriting URLs

Posted by Chris Anderson <jc...@apache.org>.
On Sat, Jul 4, 2009 at 3:12 PM, Paul Davis<pa...@gmail.com> wrote:
> On Sat, Jul 4, 2009 at 6:08 PM, Noah Slater<ns...@apache.org> wrote:
>> Hey Jason,
>>
>> On Sat, Jul 04, 2009 at 10:34:27PM +0100, Jason Davies wrote:
>>> The basic idea is you add a "rewrites": [...] member to your design doc.
>>> This will allow rewriting of any URLs with the prefix mydb/
>>> _design/app/_rewrite.  The "rewrites" member is a list of rewrite rules
>>> of the form {match: ["foo/bar/<var>"], rewrite:["_view/myview",
>>> {startkey: ["<var>"], endkey: ["<var>", {}]}]
>>
>> This is great!
>>
>> Don't let me contribute stop-energy, but I am thinking that if we're going to do
>> this, we might want to go the full way an implement this from the root URI of
>> the whole CouchDB server instead.
>>
>> Best,
>>
>> --
>> Noah Slater, http://tumbolia.org/nslater
>>
>
> We've discussed where to push the URL rewriting previously. The
> biggest argument against a DB wide rewrite scheme is that _design/
> documents could end up stomping on each other pretty easily.
>
> For reference in case other people are confused, the rewrite
> capabilities would be best used by putting a proxy in front of CouchDB
> that would rewrite all urls to the appropiate CouchDB path.
>
> Something like:
>
> Rewrite http://blog.mydomain.com/ to
> http://127.0.0.1:5984/my_blog_db/_design/sofar/_rewrite/
>

Eventually this could be implemented in CouchDB itself, with a table
to lookup app-rewrites for various subdomains or ports. I think it's
important to limit what the design doc alone can do. Putting something
like a dictionary of app -> ddoc rewrites in config seems like the
right place to modify CouchDB at such a fundamental level.

Chris



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: Rewriting URLs

Posted by Paul Davis <pa...@gmail.com>.
On Sat, Jul 4, 2009 at 6:08 PM, Noah Slater<ns...@apache.org> wrote:
> Hey Jason,
>
> On Sat, Jul 04, 2009 at 10:34:27PM +0100, Jason Davies wrote:
>> The basic idea is you add a "rewrites": [...] member to your design doc.
>> This will allow rewriting of any URLs with the prefix mydb/
>> _design/app/_rewrite.  The "rewrites" member is a list of rewrite rules
>> of the form {match: ["foo/bar/<var>"], rewrite:["_view/myview",
>> {startkey: ["<var>"], endkey: ["<var>", {}]}]
>
> This is great!
>
> Don't let me contribute stop-energy, but I am thinking that if we're going to do
> this, we might want to go the full way an implement this from the root URI of
> the whole CouchDB server instead.
>
> Best,
>
> --
> Noah Slater, http://tumbolia.org/nslater
>

We've discussed where to push the URL rewriting previously. The
biggest argument against a DB wide rewrite scheme is that _design/
documents could end up stomping on each other pretty easily.

For reference in case other people are confused, the rewrite
capabilities would be best used by putting a proxy in front of CouchDB
that would rewrite all urls to the appropiate CouchDB path.

Something like:

Rewrite http://blog.mydomain.com/ to
http://127.0.0.1:5984/my_blog_db/_design/sofar/_rewrite/

HTH,
Paul Davis

Re: Rewriting URLs

Posted by Noah Slater <ns...@apache.org>.
Hey Jason,

On Sat, Jul 04, 2009 at 10:34:27PM +0100, Jason Davies wrote:
> The basic idea is you add a "rewrites": [...] member to your design doc.
> This will allow rewriting of any URLs with the prefix mydb/
> _design/app/_rewrite.  The "rewrites" member is a list of rewrite rules
> of the form {match: ["foo/bar/<var>"], rewrite:["_view/myview",
> {startkey: ["<var>"], endkey: ["<var>", {}]}]

This is great!

Don't let me contribute stop-energy, but I am thinking that if we're going to do
this, we might want to go the full way an implement this from the root URI of
the whole CouchDB server instead.

Best,

-- 
Noah Slater, http://tumbolia.org/nslater

Re: Rewriting URLs

Posted by Benoit Chesneau <bc...@gmail.com>.
tested and worked well :)

Maybe it's better on db and global level though. On httpd level maybe
could it works on 404 not found so you won't overwrite dbname. The
same on db level ?

I'm also thinking it would be usefull to have a way to fetch rewriting
from a view . for example

match = ["<key>"],
rewrite = [{ "view": "designame/view" }]

so if value match key you retrieve values. It give some nice
possibilities like aliasing docid to a slug in doc. Something like it.
Just thinking anyway. Having _rewrite like it is help a lot already.

- benoît


2009/7/4 Jason Davies <ja...@jasondavies.com>:
> Hi all,
>
> I've mostly completed work on a patch for a _rewrite handler.  This was one
> of the main missing pieces for me when trying to provide pretty/clean URLs
> when using CouchDB to deploy pure CouchApps behind a reverse proxy (Nginx).
>  My approach up until now has been to use Nginx's mod_rewrite to rewrite
> URLs using regular expressions.  However, this is not workable for those of
> us who want to replicate CouchApps in a portable manner.
>
> The basic idea is you add a "rewrites": [...] member to your design doc.
>  This will allow rewriting of any URLs with the prefix
> mydb/_design/app/_rewrite.  The "rewrites" member is a list of rewrite rules
> of the form {match: ["foo/bar/<var>"], rewrite:["_view/myview", {startkey:
> ["<var>"], endkey: ["<var>", {}]}]
>
> This is very similar to WebMachine's routing syntax, which would be
> something like ["foo", "bar", var] for the match part.  Notice how "<var>"
> is used to denote an atom.  Atoms in the target path and in the target query
> parameters are replaced with any tokens they matched in the source rule.
>  There is a special atom '*' that can be used (as in WebMachine) to match
> any number of path elements (normally atoms match a single path element).
>
> The branch is here: http://github.com/jasondavies/couchdb/tree/rewrite
>
> And a patch here if you don't use git: http://gist.github.com/140738
>
> And a paste of part of the test case if you are too lazy to click on those
> links:
>
>    rewrites: [{
>      match: ["any_view/<view>/<key>"],
>      rewrite: ["_design/rewrite_test/_view/<view>", {key: "<key>"}]
>    },{
>      match: ["any_doc/<doc_id>/<*>"],
>      rewrite: ["<doc_id>/<*>"]
>    },{
>      match: ["design_doc/<name>/<*>"],
>      rewrite: ["_design/<name>/<*>"]
>    }]
>
> I still need to add a few more tests and think about perhaps allowing
> relative targets so that you can have a target of ../../blah or even
> /_uuids, and the possibility of special atoms e.g. <list>/mylist/myview to
> map to lists/views/shows/docs.
>
> Any thoughts much appreciated,
> --
> Jason Davies
>
> www.jasondavies.com
>
> On 8 May 2009, at 01:15, Jason Davies wrote:
>
>> On 7 May 2009, at 18:50, Jason Davies wrote:
>>
>>> On 7 May 2009, at 18:33, Justin Sheehy wrote:
>>>
>>>> I've now changed the dispatcher such that it should be useful outside
>>>> of webmachine without affecting internal use.
>>
>> [snip]
>>
>>> This looks great, thanks very much!  I'll let you know how I get on.
>>
>> Okay, here's a first stab at this:
>> http://github.com/jasondavies/couchdb/commit/f6cb4012269e5331c5bf95077a00a4a09c9ea029
>>
>> The basic idea is you add "rewrites": [...] member to the design doc.
>>  This is a list of rewrite rules of the form: {"match": [...], "rewrite":
>> [...]}.  The "match" member is a list of path tokens to be matched.  The
>> "rewrite" member specifies the target path, also as a list of path tokens.
>>
>> The match tokens can be either "foo" (specific match of "foo"), ":foo"
>> (any token, can be captured in the target path using ":foo", equivalent to
>> Erlang atom in WebMachine), or ":*" (only allowed at the end of a list,
>> matches any number of tokens, can be captured in the target path using ":*",
>> equivalent to Erlang atom '*' in WebMachine).
>>
>> The target path is analogous to the match path, just that "atoms" specify
>> parts of the match path to be captured.
>>
>> Is this moving in the right direction?
>>
>> It seems this can't cope with things like rewriting /blog/2009/05/08/foo/
>> -> /db/blog%2F2009%2F05%2F08%2Ffoo though, or any other kind of slightly
>> more flexible regex-style rewriting problems.  Any thoughts on this?
>>
>> Thanks,
>> --
>> Jason Davies
>>
>> www.jasondavies.com
>>
>
>

Re: Rewriting URLs

Posted by Jason Davies <ja...@jasondavies.com>.
On 4 Jul 2009, at 22:34, Jason Davies wrote:

> I've mostly completed work on a patch for a _rewrite handler.  This  
> was one of the main missing pieces for me when trying to provide  
> pretty/clean URLs when using CouchDB to deploy pure CouchApps behind  
> a reverse proxy (Nginx).  My approach up until now has been to use  
> Nginx's mod_rewrite to rewrite URLs using regular expressions.   
> However, this is not workable for those of us who want to replicate  
> CouchApps in a portable manner.
>
> The basic idea is you add a "rewrites": [...] member to your design  
> doc.  This will allow rewriting of any URLs with the prefix mydb/ 
> _design/app/_rewrite.  The "rewrites" member is a list of rewrite  
> rules of the form {match: ["foo/bar/<var>"], rewrite:["_view/ 
> myview", {startkey: ["<var>"], endkey: ["<var>", {}]}]
>
> This is very similar to WebMachine's routing syntax, which would be  
> something like ["foo", "bar", var] for the match part.  Notice how  
> "<var>" is used to denote an atom.  Atoms in the target path and in  
> the target query parameters are replaced with any tokens they  
> matched in the source rule.  There is a special atom '*' that can be  
> used (as in WebMachine) to match any number of path elements  
> (normally atoms match a single path element).

[snip]

Just thought of a limitation in the current code in that it only  
generates strings in the target query parameters, which isn't useful  
if you want to have keys containing booleans or numbers.  I'm thinking  
of using something similar to Werkzeug's syntax (http://werkzeug.pocoo.org/documentation/dev/routing.html 
) e.g. <number:var> or <bool:var>.  Alternatively I could have a  
modifier in the target atoms e.g. <safe:var> to indicate that the  
variable can be sent straight through without being JSON-encoded as a  
string.

--
Jason Davies

www.jasondavies.com


Rewriting URLs

Posted by Jason Davies <ja...@jasondavies.com>.
Hi all,

I've mostly completed work on a patch for a _rewrite handler.  This  
was one of the main missing pieces for me when trying to provide  
pretty/clean URLs when using CouchDB to deploy pure CouchApps behind a  
reverse proxy (Nginx).  My approach up until now has been to use  
Nginx's mod_rewrite to rewrite URLs using regular expressions.   
However, this is not workable for those of us who want to replicate  
CouchApps in a portable manner.

The basic idea is you add a "rewrites": [...] member to your design  
doc.  This will allow rewriting of any URLs with the prefix mydb/ 
_design/app/_rewrite.  The "rewrites" member is a list of rewrite  
rules of the form {match: ["foo/bar/<var>"], rewrite:["_view/myview",  
{startkey: ["<var>"], endkey: ["<var>", {}]}]

This is very similar to WebMachine's routing syntax, which would be  
something like ["foo", "bar", var] for the match part.  Notice how  
"<var>" is used to denote an atom.  Atoms in the target path and in  
the target query parameters are replaced with any tokens they matched  
in the source rule.  There is a special atom '*' that can be used (as  
in WebMachine) to match any number of path elements (normally atoms  
match a single path element).

The branch is here: http://github.com/jasondavies/couchdb/tree/rewrite

And a patch here if you don't use git: http://gist.github.com/140738

And a paste of part of the test case if you are too lazy to click on  
those links:

     rewrites: [{
       match: ["any_view/<view>/<key>"],
       rewrite: ["_design/rewrite_test/_view/<view>", {key: "<key>"}]
     },{
       match: ["any_doc/<doc_id>/<*>"],
       rewrite: ["<doc_id>/<*>"]
     },{
       match: ["design_doc/<name>/<*>"],
       rewrite: ["_design/<name>/<*>"]
     }]

I still need to add a few more tests and think about perhaps allowing  
relative targets so that you can have a target of ../../blah or even / 
_uuids, and the possibility of special atoms e.g. <list>/mylist/myview  
to map to lists/views/shows/docs.

Any thoughts much appreciated,
--
Jason Davies

www.jasondavies.com

On 8 May 2009, at 01:15, Jason Davies wrote:

> On 7 May 2009, at 18:50, Jason Davies wrote:
>
>> On 7 May 2009, at 18:33, Justin Sheehy wrote:
>>
>>> I've now changed the dispatcher such that it should be useful  
>>> outside
>>> of webmachine without affecting internal use.
>
> [snip]
>
>> This looks great, thanks very much!  I'll let you know how I get on.
>
> Okay, here's a first stab at this: http://github.com/jasondavies/couchdb/commit/f6cb4012269e5331c5bf95077a00a4a09c9ea029
>
> The basic idea is you add "rewrites": [...] member to the design  
> doc.  This is a list of rewrite rules of the form: {"match": [...],  
> "rewrite": [...]}.  The "match" member is a list of path tokens to  
> be matched.  The "rewrite" member specifies the target path, also as  
> a list of path tokens.
>
> The match tokens can be either "foo" (specific match of "foo"),  
> ":foo" (any token, can be captured in the target path using ":foo",  
> equivalent to Erlang atom in WebMachine), or ":*" (only allowed at  
> the end of a list, matches any number of tokens, can be captured in  
> the target path using ":*", equivalent to Erlang atom '*' in  
> WebMachine).
>
> The target path is analogous to the match path, just that "atoms"  
> specify parts of the match path to be captured.
>
> Is this moving in the right direction?
>
> It seems this can't cope with things like rewriting /blog/2009/05/08/ 
> foo/ -> /db/blog%2F2009%2F05%2F08%2Ffoo though, or any other kind of  
> slightly more flexible regex-style rewriting problems.  Any thoughts  
> on this?
>
> Thanks,
> --
> Jason Davies
>
> www.jasondavies.com
>


Re: Webmachine dispatcher example

Posted by Jason Davies <ja...@jasondavies.com>.
On 7 May 2009, at 18:50, Jason Davies wrote:

> On 7 May 2009, at 18:33, Justin Sheehy wrote:
>
>> I've now changed the dispatcher such that it should be useful outside
>> of webmachine without affecting internal use.

[snip]

> This looks great, thanks very much!  I'll let you know how I get on.

Okay, here's a first stab at this: http://github.com/jasondavies/couchdb/commit/f6cb4012269e5331c5bf95077a00a4a09c9ea029

The basic idea is you add "rewrites": [...] member to the design doc.   
This is a list of rewrite rules of the form: {"match": [...],  
"rewrite": [...]}.  The "match" member is a list of path tokens to be  
matched.  The "rewrite" member specifies the target path, also as a  
list of path tokens.

The match tokens can be either "foo" (specific match of "foo"),  
":foo" (any token, can be captured in the target path using ":foo",  
equivalent to Erlang atom in WebMachine), or ":*" (only allowed at the  
end of a list, matches any number of tokens, can be captured in the  
target path using ":*", equivalent to Erlang atom '*' in WebMachine).

The target path is analogous to the match path, just that "atoms"  
specify parts of the match path to be captured.

Is this moving in the right direction?

It seems this can't cope with things like rewriting /blog/2009/05/08/ 
foo/ -> /db/blog%2F2009%2F05%2F08%2Ffoo though, or any other kind of  
slightly more flexible regex-style rewriting problems.  Any thoughts  
on this?

Thanks,
--
Jason Davies

www.jasondavies.com


Re: Webmachine dispatcher example

Posted by Jason Davies <ja...@jasondavies.com>.
Hi Justin,

On 7 May 2009, at 18:33, Justin Sheehy wrote:

> I've now changed the dispatcher such that it should be useful outside
> of webmachine without affecting internal use.
>
> Check out
> http://bitbucket.org/justin/webmachine/src/tip/src/webmachine_dispatcher.erl
> if you want to see it.  As with much Erlang, the actual volume of  
> code is
> quite small.  It is all Apache2 licensed, so there should be no  
> problem if you
> want to use that module in couchdb for this purpose.
>
> The API is quite simple.  Just call webmachine_dispatcher:dispatch/2  
> with
> a path and a list of the dispatch terms I explained in the other
> email.  A trivial
> example:
>
> 1> webmachine_dispatcher:dispatch("/a",[{["a"],some_resource,[]}])
> {some_resource,[],[],[],".",[]}
>
> I suspect that you won't have much trouble turning JSON forms into
> dispatch terms.
>
> I'm interested to hear how it works out for you, and welcome any  
> suggestions.

This looks great, thanks very much!  I'll let you know how I get on.

--
Jason Davies

www.jasondavies.com


Re: Webmachine dispatcher example

Posted by Justin Sheehy <ju...@iago.org>.
I've now changed the dispatcher such that it should be useful outside
of webmachine without affecting internal use.

Check out
http://bitbucket.org/justin/webmachine/src/tip/src/webmachine_dispatcher.erl
if you want to see it.  As with much Erlang, the actual volume of code is
quite small.  It is all Apache2 licensed, so there should be no problem if you
want to use that module in couchdb for this purpose.

The API is quite simple.  Just call webmachine_dispatcher:dispatch/2 with
a path and a list of the dispatch terms I explained in the other
email.  A trivial
example:

1> webmachine_dispatcher:dispatch("/a",[{["a"],some_resource,[]}])
{some_resource,[],[],[],".",[]}

I suspect that you won't have much trouble turning JSON forms into
dispatch terms.

I'm interested to hear how it works out for you, and welcome any suggestions.

Cheers,

-Justin