Get Started with Scalatra on Scalingo

Requirements

Before doing this tutorial you should have setup your environment:

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

The main file for this sample is server.scala with the following content:

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

Commit your application

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

Create your application on Scalingo and deploy

$ scalingo create my-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://my-app.osc-fr1.scalingo.io -->

Now develop your project and build something amazing!


Suggest edits

Get Started with Scalatra on Scalingo

©2024 Scalingo