Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Scalatra on Heroku

Scalatra on Heroku

This is pretty easy to get up and running. Only thing you really need to do is start jetty directly, and add a script to execute this. You don't want to have to rely on SBT to start your application.

Easiest way to do this is create a Main method to start jetty. See JettyLauncher.scala - save this in your src/main/scala dir, setting the filter to your applications filter. Then use Typesafe's start script plugin to generate a script to start the app.

To enable the plugin, add the following to project/plugins/build.sbt

resolvers += {
    val typesafeRepoUrl = new"")
    val pattern = Patterns(false, "[organisation]/[module]/[sbtversion]/[revision]/[type]s/[module](-[classifier])-[revision].[ext]")
    Resolver.url("Typesafe Ivy Snapshot Repository", typesafeRepoUrl)(pattern)

libraryDependencies <<= (libraryDependencies, sbtVersion) { (deps, version) =>
    deps :+ ("com.typesafe.startscript" %% "xsbt-start-script-plugin" % "0.1-SNAPSHOT" extra("sbtversion" -> version))

And the following to your build.sbt

import com.typesafe.startscript.StartScriptPlugin

seq(StartScriptPlugin.startScriptForClassesSettings: _*)

Once this is done, you are ready to deploy to Heroku. Create a Procfile in the root if your project containing

web: target/start

Commit your changes to git and make sure you have the heroku gem installed. You can then create and push the app.

heroku create appname --stack cedar
git push heroku master
* Starts jetty for scalatra programatically
* Replace YourApplicationEndpointFilter with the filter in your application
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{DefaultServlet, ServletContextHandler}
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 ServletContextHandler(server, "/", ServletContextHandler.SESSIONS)
context.addFilter(classOf[YourApplicationEndpointFilter], "/*", 0)
context.addServlet(classOf[DefaultServlet], "/");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment