26 Jun, 2011
Getting started with Scala, SBT and Scalatra (with help from g8)
Posted by: Chris In: Random thoughts
We are on iteration 3 of the project and early on it was discovered that the Play framework was perhaps a little too “opinionated” in it’s ways. So much so that it dictates which version of Scala you use.
Scalatra
That wasn’t going to work out well for us so we decided to try Scalatra which is a very lightweight web framework. It’s influenced by Sinatra which is popular amongst Ruby coders. (so it must be cool huh?)
Here is a short example from the Scalatra Github
class ScalatraExample extends ScalatraServlet {
get("/") {
<h1>Hello, world!</h1>
}
get("/hello/:name") {
// Matches "GET /hello/foo" and "GET /hello/bar"
<p>Hello, {params("name")}</p>
}
}
Obviously it’s not best-practice to have your rendering code inside your controller classes so Scalatra has built in support for Scalate, which is a templating engine and let’s you choose between a few templating formats
Scalatra also seems to be pretty handy with testing and provides in-build support for ScalaTest
test("simple get") {
get("/path/to/something") {
status should equal (200)
body should include ("hi!")
}
}
The code above is pretty fricking sweet.
SBT
SBT is a Maven-esque way of setting up Scala projects. It’s still not as simple as setting up .NET projects (:p) but it’s not so bad. The configuration of the projects are written in Scala which is quite nice compared to Maven’s hideous XML files.
Getting started
A quick note that I have only tried this on *nix operating systems, Mac OS X and Fedora (what has become of me?). I’m not sure how easy or hard this is to do on Windows.
Install the latest version of Scala
Navigate in a terminal to where you want to create a project.
g8 scalatra/scalatra-sbt cd "name-of-app" sbtupdate jetty-run
Navigate to localhost:8080
If you then fire up your favourite IDE (I’m using Intellij) you will see that SBT and G8 have worked together to give you a basic project skeleton to play with.



