Skip to content
Browse files

Added route matching via a matching function

  • Loading branch information...
1 parent c7f60ac commit 2d37a47201e3a71216c1436ef6c7e3c472671e61 @chrisbu committed Apr 28, 2012
Showing with 46 additions and 11 deletions.
  1. +16 −9 README.md
  2. +25 −2 handlers/endpoints/route.dart
  3. +5 −0 test/crimsonTest.dart
View
25 README.md
@@ -15,19 +15,26 @@ Usage: See test/crimsonTest.dart for example, but it goes something like this...
main() {
CrimsonHttpServer server = new CrimsonHttpServer();
-
+
CrimsonModule sampleModule = new CrimsonModule(server);
+
sampleModule.handlers
- .addEndpoint(new Favicon("./favicon.ico")) //match the favicon request
- .addFilter(new CookieSession()) //adds session support
- .addEndpoint(new Route("/hello","GET",(req,res,data) { //execute arbitary code that matches a route
+ .addEndpoint(new Favicon("./favicon.ico")) // match the favicon request
+ .addFilter(new CookieSession()) // adds session support
+ .addEndpoint(new Route("/hello","GET",(req,res,data) { // execute arbitary code that matches a route
res.outputStream.write("Hello");
- ))
- .addEndpoint(new StaticFile("./public")); //serve static files
+ ))
+ .addEndpoint(
+ new Route.withMatcher((req) => req.path.endsWith("someString"), // matcher function returns bool
+ "customMatcherName", // matcher name for logging
+ (req,res,data) => res.outputStream.write("Hello"); // execute on match
+ )
+ )
+ .addEndpoint(new StaticFile("./public")); // serve static files
- server.modules["*"] = sampleModule; //this is the default module.
+ server.modules["*"] = sampleModule; // this is the default module.
- server.listen("127.0.0.1", 8082);
+ server.listen("127.0.0.1", 8082); // start listening
}
@@ -46,4 +53,4 @@ Usage: See test/crimsonTest.dart for example, but it goes something like this...
#Endpoints
* Favicon: Serves a favicon from either the default ./favicon.ico or ./public/favicon.ico, or some specified location.
* StaticFile: serves static files from the path provided in the constructor. Simply appends the request.uri onto whatever path you provide in, and tries to load it. Very insecure.
-* Route: executes a dart method in resposne to matching a path + method
+* Route: executes a dart method in resposne to matching a path + method, or a matching function
View
27 handlers/endpoints/route.dart
@@ -4,6 +4,8 @@ class Route implements CrimsonEndpoint {
var _method;
var _handler;
var logger;
+ var _routeItentifier;
+ var _matcher;
CrimsonHttpServer server;
///Creates the route, which will match the path and method, and pass the
@@ -18,10 +20,31 @@ class Route implements CrimsonEndpoint {
}
+ /// Allow matching with a custom _matcher function, which should return true or false.
+ /// The [routeIdentifier] is provided to provide a way to log
+ Route.withMatcher(bool this._matcher(HttpRequest req), String _routeIdentifier, Future handler(HttpRequest, HttpResponse, CrimsonData)) {
+ _name = "ROUTE:matcher:${_routeIdentifier}";
+ logger = LoggerFactory.getLogger(_name);
+ _handler = handler;
+ }
+
Future<CrimsonData> handle(HttpRequest req, HttpResponse res, CrimsonData data) {
logger.debug("Request:${req.method}:${req.path} - Handler:${this._method}:${this._path}");
- if (req.path == this._path && req.method == this._method) {
- logger.debug("Routable handler for request:${req.method}:${req.path}");
+ bool isMatched = false;
+ if (this._matcher != null) {
+ if (_matcher(req)) {
+ isMatched = true;
+ }
+ }
+ else if (req.path == this._path && req.method == this._method) {
+ isMatched = true;
+ }
+ else {
+ //TODO: Add regex matching
+ }
+
+ if (isMatched) {
+ logger.debug("Routable handler for request: ${_name}");
Completer completer = new Completer();
Future handlerComplete = _handler(req,res,data);
View
5 test/crimsonTest.dart
@@ -13,6 +13,7 @@ main() {
.addEndpoint(new Favicon("./test/favicon.ico"))
.addFilter(new CookieSession())
.addEndpoint(new Route("/hello","GET",sayHello))
+ .addEndpoint(new Route.withMatcher(matcherFunction,"helloMatcher",sayHello))
.addEndpoint(new StaticFile("./test/public"));
@@ -21,6 +22,10 @@ main() {
server.listen("127.0.0.1", 8082);
}
+bool matcherFunction(HttpRequest req) {
+ return req.path.endsWith("matcher");
+}
+
Future sayHello(HttpRequest req,HttpResponse res,var data) {
res.outputStream.writeString("Hello");
var session = data["SESSION"];

0 comments on commit 2d37a47

Please sign in to comment.
Something went wrong with that request. Please try again.