Getting Started with Scalatra

Requirements

Before doing this tutorial you should have setup your environment:

All the operation achieved with the 'scalingo' CLI can also be done on our web dashbaord.

Prerequisites

You will need to download a few utilities to initialize your Scalatra app.

Conscript is a tool for installing and updating Scala code. Giter8, which depends on conscript, allows you to check out project templates directly from Github. It’s the recommended way to generate Scalatra project skeletons.

#
# Install conscript and giter8
#

$ curl https://raw.githubusercontent.com/foundweekends/conscript/master/setup.sh | sh
$ cs foundweekends/giter8

Initialize your application

$ g8 scalatra/scalatra-sbt

#
# You need to fill the different info field for your project
#

$ cd /your/project/directory

#
# Run it locally
#

$ chmod u+x sbt
$ ./sbt
> jetty:start

To allow sbt to create a binary for your app you should have:

  • addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.0-M1") in ./project/plugins.sbt
  • enablePlugins(JavaAppPackaging) in ./project/build.scala

Fix jetty installation scope

In the file project/build.scala, your should see a line like the following, defining the dependency jetty-webapp

        "org.eclipse.jetty" % "jetty-webapp" % "9.3.9.v20160517",

You need to modify this line to add the use of the package in the compile stage:

        "org.eclipse.jetty" % "jetty-webapp" % "9.3.9.v20160517" % "compile",

Write a base server file

server.scala

// Scalatra Sample application

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{ DefaultServlet, ServletContextHandler }
import org.eclipse.jetty.webapp.WebAppContext

object JettyLauncher {
  def main(args: Array[String]) {
    val port = if(System.getenv("PORT") != null) System.getenv("PORT").toInt else 8080

    val server = new Server(port)
    val context = new WebAppContext()
    context setContextPath "/"
    context.setResourceBase("src/main/webapp")
    context.addServlet(classOf[com.example.app.MyScalatraServlet], "/*")
    context.addServlet(classOf[DefaultServlet], "/")

    server.setHandler(context)

    server.start
    server.join
  }
}
The `System.getenv("PORT")` will read the environment variable PORT dynamically provided by our container manager.

Commit your application

$ git init
$ git add .
$ git commit -m "Base Scalatra application"

Create your application on Scalingo and deploy

This operation has to be done in the directory of your project.

$ scalingo create scalatra-app
Git repository detected: remote scalingo added
→ 'git push scalingo master' to deploy your app

$ git push scalingo master

Access your application

…
Waiting for your application to boot...
<-- https://scalatra-app.scalingo.io -->

Now develop your project and build something amazing!

schedule 22 Jul 2016