Wanna build a really, really slim Play project?
This project demonstrates how you can easily build a non-blocking, threadsafe, and fast Play app without having to
use the default routes
file. The end-result should be familiar territory for people who are used to the simplicity of Sinatra/Bottle
but want to take advantage of Scala's concurrent, type-safe and scalable nature.
In case you want to get up and running right away, use the following Slim-Play templates:
For Giter8: $ g8 lloydmeta/slim-play
- Git clone this project or use a template
sbt run
from the project's root directory- Open a browser and hit:
All I did was:
- Use
sbt new
to generate a new Play app ($ sbt new playframework/play-scala-seed.g8 --name=slim-play
) - Delete the auto-generated controller, public, and view directories (won't be using them)
- Create a
AppLoader.scala
file in the./app
directory, which holds an ApplicationLoader and the router, which is super simple:
val router = Router.from {
case GET(p"/hello/$to") => Action {
Ok(s"Hello $to")
}
/*
Use Action.async to return a Future result (sqrt can be intense :P)
Note the use of double(num) to bind only numbers (built-in :)
*/
case GET(p"/sqrt/${double(num)}") => Action.async {
Future {
Ok(Math.sqrt(num).toString)
}
}
}
- Add
play.application.loader=AppLoader
to./conf/application.conf
so that Play knows to load our custom app (that contains our simple router)
The following links may be useful for further understanding on what is happening here:
- Official Play docs on String Interpolating Routing DSL
- Official Play docs on Compile-time dependency injection