Skip to content

Instantly share code, notes, and snippets.

@rweald
Created December 14, 2012 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rweald/4282076 to your computer and use it in GitHub Desktop.
Save rweald/4282076 to your computer and use it in GitHub Desktop.
Running Scalatra on Heroku

Instructions

###You will need to add the sbt-start-script plugin.

This plugin gives you a stage action which generates a script called target/start that can be used to run your application without pulling SBT into memory.

This can be accomplished by first adding the following line to project/plugins.sbt

addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.6.0")

Then add the following lines to the top of build.sbt

import com.typesafe.sbt.SbtStartScript

seq(SbtStartScript.startScriptForClassesSettings: _*)

###You will need to embed Jetty in your process so it can be run with target/start

First Add the following line to build.sbt:

seq(webSettings :_*)

Then add the following dependencies to build.sbt

"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
"org.mortbay.jetty" % "jetty" % "6.1.22" % "container",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "compile",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "compile;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))

You should now be able to import the Jetty classes necessary to start to server as part of your process

###Embedding the Jetty Server

The last step to running on Heroku is embedding the Jetty server in your process.

This can be accomplished by adding the following lines to your MAIN_CLASS which is usually located at com.your.package.ProjectName.scala:

package your.package

import your.package.subpackages._

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

object $ProjectName$ extends App {

  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[$ProjectName$Servlet], "/*")
  context.addServlet(classOf[DefaultServlet], "/")

  server.setHandler(context)

  server.start
  server.join
}

###Add web process to your procfile

Heroku should automatically detect the web process however I like to be explicit.

Add the following to your Procfile

web: target/start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment