Last active
May 23, 2016 12:35
-
-
Save tabdulradi/d95e397224e00e8ae4ce to your computer and use it in GitHub Desktop.
Cake Blog: All you need to know about Play SIRD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
play.crypto.secret = "test" | |
play.crypto.secret = ${?APPLICATION_SECRET} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val service1 = Router.from { | |
case GET(p"hello/$to") => ??? | |
} | |
val service2 = Router.from { | |
case GET(p"echo/$msg") => ??? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompositeRouter(routers: Seq[Router]) extends SimpleRouter { | |
override def documentation: Seq[(String, String, String)] = | |
routers.flatMap(_.documentation) | |
override def routes: Router.Routes = | |
routers.map(_.routes).fold(Router.empty.routes)(_ orElse _) | |
} | |
object CompositeRouter { | |
def fromPrefixedRoutes(routers: (String, Router.Routes)*): CompositeRouter = | |
new CompositeRouter(routers.map { | |
case (prefix, routes) => | |
Router.from(routes).withPrefix(prefix) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CompositeRouter( | |
"service1" -> service1.routes, | |
"service2" -> service2.routes | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class PostId(id: String){ require(id.length == 3 } | |
implicit object bindablePostId extends Parsing[PostId]( | |
PostId.apply, | |
_.id, | |
(key: String, e: Exception) => s"$key is not correct PostId" | |
) | |
val postId = new PathBindableExtractor[PostId] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Pagination(page: Int, limit: Int) | |
object pagination extends QueryStringParameterExtractor[Pagination] { | |
override def unapply(qs: QueryString) = qs match { | |
case q"page=${int(page)}" & q_?"limit=${int(limit)}" => | |
Some(Pagination(page, limit.getOrElse(10))) | |
case _ => | |
None | |
} | |
} | |
val routes: Router.Routes = { | |
case GET(p"/posts/" ? pagination(p)) => Action { | |
Results.Ok(s"Posts: page = ${p.page} limit = ${p.limit}") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val server = NettyServer.fromRouter() { | |
case GET(p"/posts/") => Action { | |
Results.Ok(”All posts") | |
} | |
case GET(p"/posts/$id") => Action { | |
Results.Ok(“Post:" + id ) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new MyCompositePlayService( | |
"/service1" -> service1Routes, | |
"/service2" -> service2Routes | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyService.scala:19: Unexpected text at end of query string extractor: ‘<[0-9]{3}>’ | |
case GET(p"/posts/" ? q"id=$id<[0-9]{3}>") => | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── build.sbt | |
├── src/ | |
└── main/ | |
├── resources/ | |
└── scala/ | |
└── mypackage/ | |
└── Main.scala |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello!
how to run the server?
object StartServer {
def main(args: Array[String]) {
val server = NettyServer.fromRouter() {
case GET(p"/posts") => Action {
Results.Ok("All posts")
}
case GET(p"/post/$id") => Action {
Results.Ok("Post " + id)
}
}
}
}