Skip to content

Instantly share code, notes, and snippets.

@lstoll
Created September 11, 2011 07:13
Show Gist options
  • Save lstoll/1209277 to your computer and use it in GitHub Desktop.
Save lstoll/1209277 to your computer and use it in GitHub Desktop.
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 java.net.URL("http://repo.typesafe.com/typesafe/ivy-snapshots")
    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], "/");
context.setResourceBase("src/main/webapp")
server.start
server.join
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment