Skip to content

Instantly share code, notes, and snippets.

@tabdulradi
Last active May 23, 2016 12:35
Show Gist options
  • Save tabdulradi/d95e397224e00e8ae4ce to your computer and use it in GitHub Desktop.
Save tabdulradi/d95e397224e00e8ae4ce to your computer and use it in GitHub Desktop.
Cake Blog: All you need to know about Play SIRD
play.crypto.secret = "test"
play.crypto.secret = ${?APPLICATION_SECRET}
val service1 = Router.from {
case GET(p"hello/$to") => ???
}
val service2 = Router.from {
case GET(p"echo/$msg") => ???
}
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)
})
}
CompositeRouter(
"service1" -> service1.routes,
"service2" -> service2.routes
)
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]
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}")
}
}
val server = NettyServer.fromRouter() {
case GET(p"/posts/") => Action {
Results.Ok(”All posts")
}
case GET(p"/posts/$id") => Action {
Results.Ok(“Post:" + id )
}
}
new MyCompositePlayService(
"/service1" -> service1Routes,
"/service2" -> service2Routes
)
MyService.scala:19: Unexpected text at end of query string extractor: ‘<[0-9]{3}>’
case GET(p"/posts/" ? q"id=$id<[0-9]{3}>") =>
^
.
├── build.sbt
├── src/
└── main/
├── resources/
└── scala/
└── mypackage/
└── Main.scala
@lshoo
Copy link

lshoo commented Mar 22, 2016

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)
}
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment