couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benoit Chesneau <bchesn...@gmail.com>
Subject _rewrite handler
Date Fri, 04 Dec 2009 16:30:43 GMT
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