couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vlad GURDIGA <gurd...@gmail.com>
Subject Re: _rewrite handler
Date Sat, 05 Dec 2009 01:14:25 GMT
This thing was long awaited!

Thank you for the effort, Benoît!

On Fri, Dec 4, 2009 at 6:30 PM, Benoit Chesneau <bchesneau@gmail.com> wrote:
> Hi all,
>
> I created a branch*  based on trunk on my Github repository that
> allows url rewriting in CouchDB:
>
> http://github.com/benoitc/couchdb/tree/rewrite
>
> The design is very simple. Everything is managed via a simple
> javascript function that return a path or throw 'forbidden',
> 'unauthorized' and "not found" errors. The request object is passed to
> the function and then depending on the path, verb or userCtx you could
> decide how to rewrite the path. All relatives path are relative to
> design doc.
>
>  The _rewriter is available at db or _design level :
>
> /db/_rewrite/designname/path
>
> or
>
> /db/_design/designame/_rewrite/path.
>
>
> Then _rewrite handler look in design doc if `rewrite` member exists
> and load the function.
>
> Little snippets from tests :
>
> function(req) {
>    path = "/" + req.path.join('/');
>
>    if (path == "/forbidden") {
>        throw {
>            forbidden:
>            "path forbidden"
>        };
>    }
>    if (path == "/unauthorized") {
>        throw {
>            unauthorized:
>            "path not authorized"
>        };
>    }
>    if (path == "/notfound") {
>        throw {
>            not_found:
>            "path not found"
>        };
>    }
>
>    if (path == "/foo") {
>        return "foo.txt"
>    }
>
>    var matches = path.match(/^\/hello\/(\w.+)$/);
>
>    if (matches[1]) {
>        if (req.verb == "DELETE") {
>            throw {
>                forbidden:
>                "delete is forbidden"
>            };
>        }
>        return "_update/hello/" + matches[1];
>    }
> }
>
>
> let me know if it's ok for you,
>
> - benoît
>

Mime
View raw message