Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
New feature: If a route has matchSubPaths = true, the requested sub…
Browse files Browse the repository at this point in the history
…path can be assigned to a parameter
  • Loading branch information
luizmineo committed Aug 12, 2014
1 parent cdd3abb commit 51b1952
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.5.12
* New feature: If a route has `matchSubPaths = true`, the requested subpath can be assigned to a parameter (see issue #36).

## v0.5.11
* Minor performance fix: Redstone.dart shouldn't create a new `shelf.Pipeline` per request.

Expand Down
2 changes: 1 addition & 1 deletion lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ void setUp([List<Symbol> libraries]) {
_scanHandlers(libraries);
} catch (e) {
_handleError("Failed to configure handlers.", e);
throw e;
rethrow;
}
}

Expand Down
37 changes: 24 additions & 13 deletions lib/src/setup_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,13 @@ class _Target {
UrlMatch match(Uri uri) {
UrlMatch match = urlTemplate.match(uri.path);
if (match != null) {
if (route.matchSubPaths) {
if (uri.path.endsWith("/") || match.tail.startsWith("/")) {
return match;
}
} else {
if (match.tail.isEmpty) {
return match;
}
if (match.tail.isEmpty) {
return match;
} else if (route.matchSubPaths &&
(uri.path.endsWith("/") || match.tail.startsWith("/"))) {
return match;
}
}

if (match != null && match.tail.isEmpty) {
return match;
}
return null;
}

Expand Down Expand Up @@ -721,6 +714,7 @@ void _configureTarget(_ServerMetadataImpl serverMetadata,

var responseProcessors = null;
var wrapper = null;
var specialPathParam = null;

var caller = (UrlMatch match, Request request) {

Expand Down Expand Up @@ -771,6 +765,10 @@ void _configureTarget(_ServerMetadataImpl serverMetadata,
return new Future(() {

var pathParams = match.parameters;

if (specialPathParam != null) {
pathParams[specialPathParam] = pathParams[specialPathParam] + match.tail;
}

var posParams = [];
var namedParams = {};
Expand Down Expand Up @@ -815,12 +813,25 @@ void _configureTarget(_ServerMetadataImpl serverMetadata,

};

String url = route.urlTemplate;
String url = route.urlTemplate.trim();
if (urlPrefix != null) {
url = _joinUrl(urlPrefix, url);
}

var originalUrl = url;
if (url.endsWith("*")) {
url = url.substring(0, url.length - 1);
}

UrlTemplate urlTemplate = new UrlTemplate(url);

if (route.matchSubPaths && urlTemplate.urlParameterNames().isNotEmpty) {
var lastParam = urlTemplate.urlParameterNames().last;
if (originalUrl.endsWith(":$lastParam*")) {
specialPathParam = lastParam;
}
}

_Target target;
if (paramProcessors.bodyType == null) {
target = new _Target(urlTemplate, handlerName, caller, route);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: redstone
version: 0.5.11
version: 0.5.12
author: Luiz Mineo <[email protected]>
description: A metadata driven microframework for Dart
homepage: http://redstonedart.org
Expand All @@ -9,7 +9,7 @@ dependencies:
collection: '>=0.9.3+1 <0.10.0'
crypto: '>=0.9.0 <0.10.0'
grinder: '>=0.5.2 <0.6.0'
route_hierarchical: '>=0.4.21 <0.5.0'
route_hierarchical: '>=0.4.22 <0.5.0'
di: ">=2.0.1 <3.0.0"
shelf: ">=0.5.4+2 <0.6.0"
mime: ">=0.9.0+1 <0.10.0"
Expand Down
6 changes: 6 additions & 0 deletions test/server_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ main() {
var req = new MockRequest("/path/subpath");
var req2 = new MockRequest("/path/anotherpath");
var req3 = new MockRequest("/paths");
var req4 = new MockRequest("/path2/sub/path");
var req5 = new MockRequest("/path3/sub/path");

return app.dispatch(req).then((resp) {
expect(resp.mockContent, equals("sub_route"));
}).then((_) => app.dispatch(req2)).then((resp) {
expect(resp.mockContent, equals("main_route"));
}).then((_) => app.dispatch(req3)).then((resp) {
expect(resp.statusCode, equals(404));
}).then((_) => app.dispatch(req4)).then((resp) {
expect(resp.mockContent, equals("sub"));
}).then((_) => app.dispatch(req5)).then((resp) {
expect(resp.mockContent, equals("sub/path"));
});
});

Expand Down
6 changes: 6 additions & 0 deletions test/services/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ mainRoute() => "main_route";
@app.Route("/path/subpath")
subRoute() => "sub_route";

@app.Route("/path2/:param", matchSubPaths: true)
mainRouteWithParam(String param) => param;

@app.Route("/path3/:param*", matchSubPaths: true)
mainRouteWithSpecialParam(String param) => param;

@app.Route("/handler_by_method")
getHandler() => "get_handler";

Expand Down

0 comments on commit 51b1952

Please sign in to comment.