Introducing vibe.d!
Sönke Ludwig
sludwig at outerproduct.org
Fri Apr 27 04:18:17 PDT 2012
Am 27.04.2012 11:57, schrieb David:
> I am not sure if you're aware of Flask, Flask is a microframework for
> Python.
>
> It provides something called "Blueprints", you can register e.g.
> "routes" to this Blueprint and to use them you've to add them to the
> main application. This makes code way more readable!
> Also some kind of after_request and before_request handlers would be
> great, you could use them e.g. fetching a user from the database and
> providing it to the handler (over a thread-local?).
I didn't know flask before and the website seems to be down currently ;)
But is the UrlRouter (see http://vibed.org/docs#http-routing and
http://vibed.org/api/vibe.http.router#UrlRouter) something equivalent?
It allows you to specify multiple patterns that are matched in sequence
until a request handler writes a response:
auto r = new UrlRouter;
r.any("*", &performAuth);
r.get("/", &renderHomePage);
listenHttp(settings, r);
This would first always match performAuth, which could check the user
credentials and write out a message if the user is not authenticated.
Otherwise it just does nothing and the router will continue to the next
match, which is renderHomePage() in this case for "GET /".
Whats not directly possible in the router is something like
after_request. But you can do it easily by wrapping the call:
auto r = new UrlRouter;
//...
listenHttp(settings, (req, res){
before_request(req, res);
r.handleRequest(req, res);
after_request(req, res);
});
More information about the Digitalmars-d-announce
mailing list