Skip to content

Commit

Permalink
[WIP] Reuses a matcher with then in Route.
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed Jul 16, 2015
1 parent 36ed800 commit 66557c5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 48 deletions.
7 changes: 5 additions & 2 deletions examples/app/app.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ mcd.add_server ("127.0.0.1", 11211);
app.types["permutations"] = /abc|acb|bac|bca|cab|cba/;

// default route
app.get ("", (req, res) => {
app.get ("", (req, res, next) => {
// reuse the same matcher...
res.headers.set_content_type ("text/html", null);
next ();
}).then ((req, res) => {
var template = new View.from_stream (resources_open_stream ("/templates/home.html", ResourceLookupFlags.NONE));

template.to_stream (res.body);
});

Expand Down
25 changes: 19 additions & 6 deletions src/route.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ namespace Valum {
*/
public weak Router router { construct; get; }

/**
* @since 0.3
*/
public string? method { construct; get; }

/**
* Create a Route using a custom matcher.
*
Expand All @@ -35,8 +40,8 @@ namespace Valum {
*
* @since 0.1
*/
public Route (Router router, MatcherCallback matcher, HandlerCallback callback) {
Object (router: router);
public Route (Router router, string? method, MatcherCallback matcher, HandlerCallback callback) {
Object (router: router, method: method);
this.match = matcher;
this.fire = callback;
}
Expand All @@ -54,8 +59,8 @@ namespace Valum {
*
* @since 0.1
*/
public Route.from_regex (Router router, Regex regex, HandlerCallback callback) throws RegexError {
Object (router: router);
public Route.from_regex (Router router, string? method, Regex regex, HandlerCallback callback) throws RegexError {
Object (router: router, method: method);
this.fire = callback;

var pattern = new StringBuilder ("^");
Expand Down Expand Up @@ -117,8 +122,8 @@ namespace Valum {
* @param rule compiled down ot a regular expression and captures all
* paths if set to null
*/
public Route.from_rule (Router router, string? rule, HandlerCallback callback) throws RegexError {
Object (router: router);
public Route.from_rule (Router router, string? method, string? rule, HandlerCallback callback) throws RegexError {
Object (router: router, method: method);
this.fire = callback;

var param_regex = new Regex ("(<(?:\\w+:)?\\w+>)");
Expand Down Expand Up @@ -194,5 +199,13 @@ namespace Valum {
* @since 0.0.1
*/
public HandlerCallback fire;

/**
* Pushes the handler in the {@link Router} queue to produce a sequence
* of callbacks that reuses the same matcher.
*/
public Route then (HandlerCallback handler) {
return this.router.matcher (this.method, match, handler);
}
}
}
56 changes: 29 additions & 27 deletions src/router.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,66 +48,66 @@ namespace Valum {
/**
* @since 0.0.1
*/
public new void get (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.GET, rule, cb);
public new Route get (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.GET, rule, cb);
}

/**
* @since 0.0.1
*/
public void post (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.POST, rule, cb);
public Route post (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.POST, rule, cb);
}

/**
* @since 0.0.1
*/
public void put (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.PUT, rule, cb);
public Route put (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.PUT, rule, cb);
}

/**
* @since 0.0.1
*/
public void delete (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.DELETE, rule, cb);
public Route delete (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.DELETE, rule, cb);
}

/**
* @since 0.0.1
*/
public void head (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.HEAD, rule, cb);
public Route head (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.HEAD, rule, cb);
}

/**
* @since 0.0.1
*/
public void options(string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.OPTIONS, rule, cb);
public Route options (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.OPTIONS, rule, cb);
}

/**
* @since 0.0.1
*/
public void trace (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.TRACE, rule, cb);
public Route trace (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.TRACE, rule, cb);
}

/**
* @since 0.0.1
*/
public new void connect (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.CONNECT, rule, cb);
public new Route connect (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.CONNECT, rule, cb);
}

/**
* [[http://tools.ietf.org/html/rfc5789]]
*
* @since 0.0.1
*/
public void patch (string? rule, HandlerCallback cb) throws RegexError {
this.method (Request.PATCH, rule, cb);
public Route patch (string? rule, HandlerCallback cb) throws RegexError {
return this.method (Request.PATCH, rule, cb);
}

/**
Expand All @@ -122,8 +122,8 @@ namespace Valum {
* @param rule rule
* @param cb callback used to process the pair of request and response.
*/
public void method (string method, string? rule, HandlerCallback cb) throws RegexError {
this.route (method, new Route.from_rule (this, rule, cb));
public Route method (string method, string? rule, HandlerCallback cb) throws RegexError {
return this.route (method, new Route.from_rule (this, method, rule, cb));
}

/**
Expand All @@ -144,7 +144,7 @@ namespace Valum {
* @param rule rule
*/
public void methods (string[] methods, string? rule, HandlerCallback cb) throws RegexError {
var route = new Route.from_rule (this, rule, cb);
var route = new Route.from_rule (this, null, rule, cb);
foreach (var method in methods) {
this.route (method, route);
}
Expand All @@ -162,8 +162,8 @@ namespace Valum {
* @param regex regular expression matching the request path.
* @param cb callback used to process the pair of request and response.
*/
public void regex (string method, Regex regex, HandlerCallback cb) throws RegexError {
this.route (method, new Route.from_regex (this, regex, cb));
public Route regex (string method, Regex regex, HandlerCallback cb) throws RegexError {
return this.route (method, new Route.from_regex (this, method, regex, cb));
}

/**
Expand All @@ -175,8 +175,8 @@ namespace Valum {
* @param matcher callback used to match the request
* @param cb callback used to process the pair of request and response.
*/
public void matcher (string method, MatcherCallback matcher, HandlerCallback cb) {
this.route (method, new Route (this, matcher, cb));
public Route matcher (string method, MatcherCallback matcher, HandlerCallback cb) {
return this.route (method, new Route (this, method, matcher, cb));
}

/**
Expand All @@ -188,11 +188,13 @@ namespace Valum {
* @param route an instance of Route defining the matching process and the
* callback.
*/
private void route (string method, Route route) {
private Route route (string method, Route route) {
if (!this.routes.contains (method))
this.routes[method] = new Queue<Route> ();

this.routes[method].push_tail (route);

return route;
}

/**
Expand All @@ -208,7 +210,7 @@ namespace Valum {
if (!this.status_handlers.contains (status))
this.status_handlers[status] = new Queue<Route> ();

this.status_handlers[status].push_tail (new Route (this, () => { return true; }, cb));
this.status_handlers[status].push_tail (new Route (this, null, () => { return true; }, cb));
}

/**
Expand Down
26 changes: 13 additions & 13 deletions tests/test_route.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using VSGI.Test;
*/
public void test_route () {
var router = new Router ();
var route = new Route (router, (req) => { return true; }, (req, res) => {});
var route = new Route (router, "GET", (req) => { return true; }, (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/5"));

assert (route.match (req));
Expand All @@ -17,7 +17,7 @@ public void test_route () {
* @since 0.1
*/
public void test_route_from_rule () {
var route = new Route.from_rule (new Router (), "<int:id>", (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", "<int:id>", (req, res) => {});
var request = new Request.with_uri (new Soup.URI ("http://localhost/5"));

assert (route.match (request));
Expand All @@ -29,7 +29,7 @@ public void test_route_from_rule () {
* @since 0.1
*/
public void test_route_from_rule_null () {
var route = new Route.from_rule (new Router (), null, (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", null, (req, res) => {});
var request = new Request.with_uri (new Soup.URI ("http://localhost/5"));

assert (route.match (request));
Expand All @@ -42,7 +42,7 @@ public void test_route_from_rule_null () {
* @since 0.1
*/
public static void test_route_from_rule_null_matches_empty_path () {
var route = new Route.from_rule (new Router (), null, (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", null, (req, res) => {});
var request = new Request.with_uri (new Soup.URI ("http://localhost/"));

assert (route.match (request));
Expand All @@ -52,7 +52,7 @@ public static void test_route_from_rule_null_matches_empty_path () {
* @since 0.1
*/
public void test_route_from_rule_any () {
var route = new Route.from_rule (new Router (), "<any:id>", (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", "<any:id>", (req, res) => {});
var request = new Request.with_uri (new Soup.URI ("http://localhost/5"));

assert (route.match (request));
Expand All @@ -64,7 +64,7 @@ public void test_route_from_rule_any () {
* @since 0.1
*/
public void test_route_from_rule_without_captures () {
var route = new Route.from_rule (new Router (), "", (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", "", (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/"));

assert (req.params == null);
Expand All @@ -80,14 +80,14 @@ public void test_route_from_rule_without_captures () {
* @since 0.1
*/
public void test_route_from_rule_undefined_type () {
var route = new Route.from_rule (new Router (), "<uint:unknown_type>", (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", "<uint:unknown_type>", (req, res) => {});
}

/**
* @since 0.1
*/
public void test_route_from_regex () {
var route = new Route.from_regex (new Router (), /(?<id>\d+)/, (req, res) => {});
var route = new Route.from_regex (new Router (), "GET", /(?<id>\d+)/, (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/5"));

assert (req.params == null);
Expand All @@ -107,7 +107,7 @@ public void test_route_from_regex_scoped () {

router.scopes.push_tail ("test");

var route = new Route.from_regex (router, /(?<id>\d+)/, (req, res) => {});
var route = new Route.from_regex (router, "GET", /(?<id>\d+)/, (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/test/5"));

assert (req.params == null);
Expand All @@ -123,7 +123,7 @@ public void test_route_from_regex_scoped () {
* @since 0.1
*/
public void test_route_from_regex_without_captures () {
var route = new Route.from_regex (new Router (), /.*/, (req, res) => {});
var route = new Route.from_regex (new Router (), "GET", /.*/, (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/"));

var matches = route.match (req);
Expand All @@ -137,7 +137,7 @@ public void test_route_from_regex_without_captures () {
* @since 0.1
*/
public void test_route_match () {
var route = new Route.from_rule (new Router (), "<int:id>", (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", "<int:id>", (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/5"));

assert (req.params == null);
Expand All @@ -153,7 +153,7 @@ public void test_route_match () {
* @since 0.1
*/
public void test_route_match_not_matching () {
var route = new Route.from_rule (new Router (), "<int:id>", (req, res) => {});
var route = new Route.from_rule (new Router (), "GET", "<int:id>", (req, res) => {});
var req = new Request.with_uri (new Soup.URI ("http://localhost/home"));

// no match and params remains null
Expand All @@ -166,7 +166,7 @@ public void test_route_match_not_matching () {
*/
public void test_route_fire () {
var setted = false;
var route = new Route.from_rule (new Router (), "<int:id>", (req, res) => {
var route = new Route.from_rule (new Router (), "GET", "<int:id>", (req, res) => {
setted = true;
});
var req = new Request.with_uri (new Soup.URI ("http://localhost/home"));
Expand Down

0 comments on commit 66557c5

Please sign in to comment.