On Wednesday, 12 September 2012 at 17:01:19 UTC, Sönke Ludwig
wrote:

Am 12.09.2012 16:10, schrieb Eldar Insafutdinov:

Hi all!

I've been thinking about routing lately and I think that the
system can
be made more generic. There can be various way of matching an
URL like
the current way, regexps etc. We should start by providing the
very
basic functionality - API that expects a predicate:

enum HTTP {
    GET,
    POST,
    UPDATE,
    DELETE
}

class Router {
    ...
    Router match(HTTP reqType, bool delegate(string) 

predicate,
HttpRequestHandler);

}
auto router = new Router;
router.match(HTTP.GET, (url) => url == "/", &handleIndex);

Having the most generic API we can provide a more specific
implementations like the current or regex:

class Router {
    ...
    Router match(HTTP reqType, string pattern, 

HttpRequestHandler);

Router match(HTTP reqType, Regex!char pattern, 

HttpRequestHandler);

}

Also, using enumeration for request type rather than dedicated
methods
get(), post() etc is more flexible. Does it look good and does
anyone
have any other thoughts? If yes, I will then go ahead with it
and submit
a pull request.

I was thinking about implementing a state machine based
matching of
routes at some point to improve performance with many routes.
That would
collide a bit with opaque callbacks. But maybe the performance
will be
sufficient for almost any application anyway... I think it
would be good
to make a quick benchmark to see how the number of routes
affects the
total request time before committing to this approach.

How many paths are you thinking here? tens or hundreds?

Btw. router.match() is the same as router.addRoute() is now,
but 'match'
seems like a nice naming alternative.

Yeah, I already noticed that that there is addRoute, so I'll
rename it.