Permalink
Browse files

introducing Through and Routes

  • Loading branch information...
1 parent 4ec6f09 commit a73661b335f1ba3268048a05f1ebec37592c09af @pk11 pk11 committed Feb 27, 2012
View
@@ -18,11 +18,41 @@ and then in your own package::
val o = QueryString(qs,"foo").getOrElse("noh")
Ok(<h1>It works!, query String {o}</h1>).as("text/html")
}
+ case GET(Path("/flowers")) => Action{
+ Ok(<h1>It works for flowers!</h1>).as("text/html")
+ }
}
}
The API is based on the always awesome `Unfiltered <http://unfiltered.databinder.net/Unfiltered.html>`_ library.
+Extending the Extractor based approach with regex or simple matching is possible too::
+
+ object App extends com.typesafe.play.mini.Application {
+ def route = Routes(
+ Through("/people/(.*)".r) {groups: List[String] =>
+ Action{
+ val id :: Nil = groups
+ Ok(<h1>It works with regex!, id: {id}</h1>).as("text/html")
+ }
+ },
+ {case GET(Path("/coco")) & QueryString(qs) => Action{ request =>
+ println(request.body)
+ println(play.api.Play.current)
+ val result = QueryString(qs,"foo").getOrElse("noh")
+ Ok(<h1>It works!, query String {result}</h1>).as("text/html") }
+ },
+ Through("/flowers/id/") {groups: List[String] =>
+ Action{
+ val id :: Nil = groups
+ Ok(<h1>It works with simple startsWith! - id: {id}</h1>).as("text/html")
+ }
+ }
+ )
+ }
+
+for more information on extractors, regex and Routes, please `see <https://github.com/typesafehub/play2-mini/tree/master/src/main/scala/com/typesafe/play/mini>`_
+
Other than this, there are many useful utilities available at your fingertip, take a look at the official guide of `Play for Scala Developers <https://github.com/playframework/Play20/wiki/ScalaHome>`_
@@ -67,12 +97,12 @@ G8 Project templates
java::
-g8 pk11/play-mini-java.g8
+ g8 pk11/play-mini-java.g8
scala::
-g8 pk11/play-mini-scala.g8
+ g8 pk11/play-mini-scala.g8
@@ -8,12 +8,24 @@ import play.api.mvc.Results._
* this application is registered via Global
*/
object App extends Application {
- def route = {
- case GET(Path("/coco")) & QueryString(qs) => Action{ request=>
- println(request.body)
- println(play.api.Play.current)
- val result = QueryString(qs,"foo").getOrElse("noh")
- Ok(<h1>It works!, query String {result}</h1>).as("text/html")
- }
- }
+ def route = Routes(
+ Through("/people/(.*)".r) {groups: List[String] =>
+ Action{
+ val id :: Nil = groups
+ Ok(<h1>It works with regex!, id: {id}</h1>).as("text/html")
+ }
+ },
+ {case GET(Path("/coco")) & QueryString(qs) => Action{ request =>
+ println(request.body)
+ println(play.api.Play.current)
+ val result = QueryString(qs,"foo").getOrElse("noh")
+ Ok(<h1>It works!, query String {result}</h1>).as("text/html") }
+ },
+ Through("/flowers/id/") {groups: List[String] =>
+ Action{
+ val id :: Nil = groups
+ Ok(<h1>It works with simple startsWith! - id: {id}</h1>).as("text/html")
+ }
+ }
+ )
}
@@ -0,0 +1,41 @@
+/**
+ *
+ * Copyright (C) 2011 Typesafe Inc. <http://www.typesafe.com>
+ *
+ **/
+
+package com.typesafe.play.mini
+
+import play.api.mvc._
+
+/**
+ * provides chaining for a PartialFunction[RequestHeader, Handler]
+ * example:
+ * {{{
+ * def route = Routes(
+ * Through("/people/(.*)".r) {groups: List[String] =>
+ * Action{
+ * val id :: Nil = groups
+ * Ok(<h1>It works with regex!, id: {id}</h1>).as("text/html")
+ * }
+ * },
+ * {case GET(Path("/coco")) & QueryString(qs) => Action{ request =>
+ * println(request.body)
+ * println(play.api.Play.current)
+ * val result = QueryString(qs,"foo").getOrElse("noh")
+ * Ok(<h1>It works!, query String {result}</h1>).as("text/html") }
+ * },
+ * Through("/flowers/id/") {groups: List[String] =>
+ * Action{
+ * val id :: Nil = groups
+ * Ok(<h1>It works with simple startsWith! - id: {id}</h1>).as("text/html")
+ * }
+ * }
+ * )
+ * }}}
+ */
+object Routes{
+ def apply(funcList: PartialFunction[RequestHeader, Handler]*) = {
+ funcList.toList.reduceLeft { (functions,f) => functions orElse f }
+ }
+}
@@ -0,0 +1,69 @@
+/**
+ *
+ * Copyright (C) 2011 Typesafe Inc. <http://www.typesafe.com>
+ *
+ **/
+
+package com.typesafe.play.mini
+
+import play.api.mvc._
+
+/**
+ * provides extra routing mechanism
+ **/
+object Through {
+
+
+ /**
+ * @param matches regex and extract the groups into groups
+ * @param block block of code to be executed on matching regex
+ * @return PartialFunction to be executed
+ * for incomig url: /people/25
+ * {{{
+ * def route = Through("/people/(.*)".r) { (groups: List[String]) =>
+ * Action{
+ * val id :: Nil = groups
+ * Ok("current id:"+id)
+ * }
+ * }
+ *
+ * Additionally,
+ * {{{
+ * Routes(Through(...), Through(...))
+ * }}}
+ * can be used for multiple PartialFunction
+ *
+*/
+ def apply(regex: scala.util.matching.Regex)(block: List[String] => Handler): PartialFunction[RequestHeader, Handler] = {
+ case rh: RequestHeader if regex.findFirstMatchIn(Path(rh)).isDefined =>
+ block(regex.unapplySeq(Path(rh)).getOrElse(Nil))
+ }
+
+ /**
+ * @param baseUrl matches the baseUrl from the left and returning and returning the result split by "/"
+ * @param block to be executed on matching regex
+ * example
+ * for incomig url: /people/my/current/25
+ * {{{
+ * def route = Through("/people/) {groups: List[String] =>
+ * Action{
+ * val id :: Nil = groups
+ * Ok(<h1>It works with startsWith!, id: {id}</h1>).as("text/html")
+ * }
+ * }
+ * }}}
+ *
+ * Additionally,
+ * {{{
+ * Routes(Through(...), Through(...))
+ * }}}
+ * can be used for multiple PartialFunction
+ *
+*/
+ def apply(baseUrl: String)(block: List[String] => Handler): PartialFunction[RequestHeader, Handler] = {
+ case rh: RequestHeader if Path(rh).startsWith(baseUrl) =>
+ val groups = Path(rh).split(baseUrl)(1).split("/").toList
+ block(groups)
+ }
+
+}
@@ -33,52 +33,7 @@ object QueryString {
}
}
-/**
- * provides alternative routing mechanism where
- **/
-object Through {
-
-
- /**
- * for incomig url: /people/25
- * {{{
- * def route = Through("/people/(.*)".r) { (groups: List[String]) =>
- * Action{
- * val id :: Nil = groups
- * Ok("current id:"+id)
- * }
- * } orElse {
- * case GET(Path("/internalnotgoinganywhere")) => Action{Ok("yay")}
- * }
- * }
- * }}}
-*/
- def apply(regex: scala.util.matching.Regex)(rf: List[String] => Handler): PartialFunction[RequestHeader, Handler] = {
- case rh: RequestHeader if regex.findFirstMatchIn(Path(rh)).isDefined =>
- rf(regex.unapplySeq(Path(rh)).getOrElse(Nil))
- }
- /**
- * for incomig url: /people/my/current/25
- * {{{
- * def route = Through("/people/"){ (groups: List[String]) =>
- * Action{
- * val "my" :: "current" :: id = groups
- * Ok("current id:"+id)
- * }
- * } orElse {
- * case GET(Path("/internalnotgoinganywhere")) => Action{Ok("yay")}
- * }
- * }
- * }}}
-*/
- def apply(baseUrl: String)(rf: List[String] => Handler): PartialFunction[RequestHeader, Handler] = {
- case rh: RequestHeader if Path(rh).startsWith(baseUrl) =>
- val groups = Path(rh).split(baseUrl)(1).split("/").toList
- rf(groups)
- }
-
-}
/**
* path segment
*/
@@ -108,5 +63,3 @@ object OPTIONS extends Method("OPTIONS")
object TRACE extends Method("TRACE")
object & { def unapply[A](a: A) = Some(a, a) }
-
-

0 comments on commit a73661b

Please sign in to comment.