casualjim
9/10/2011 - 1:30 PM

.gitignore

package org.scalatra

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.handler._
import org.eclipse.jetty.webapp.WebAppContext
import org.eclipse.jetty.server.nio.SelectChannelConnector
import org.eclipse.jetty.servlet._
import tools.nsc.interpreter._
import tools.nsc.Settings
import io.Source
import java.io.File


object ScalatraMain extends App {
  trait PlaceHolder

  println("Please wait while scalatra gets ready for business...")
  val code = """
  import org.scalatra._
  class HelloServlet extends ScalatraServlet {

%s
    
  }
  """

  val fileContent = Source.fromFile(new File("./" + args(0) + ".scalatra")).getLines().filter(_.nonEmpty).map("  " + _).mkString("\n  ")
  val fullCode = code format fileContent

  val server = new Server
  server setGracefulShutdown 5000

  val connector = new SelectChannelConnector
  connector setPort 4567
  connector setMaxIdleTime 90000
  server addConnector connector

  val settings = new Settings()
  settings.embeddedDefaults(getClass.getClassLoader)
  val i = new IMain(settings)
  i.compileString(fullCode)

  class ServletLoader {
    var app: ScalatraServlet = null
  }
  val app = new ServletLoader
  i beQuietDuring {
    i.bind[ServletLoader]("scalatraApp", app)
    i.interpret("scalatraApp.app = new HelloServlet")
  }

  val dynContext = new WebAppContext
  dynContext setContextPath "/"
  dynContext setResourceBase "."
  dynContext setParentLoaderPriority true
  dynContext addServlet (new ServletHolder(app.app), "/*")
  dynContext addServlet (new ServletHolder(new DefaultServlet()), "/")

  val contexts = new ContextHandlerCollection
  contexts setHandlers Array(dynContext)
  server setHandler contexts

  println("Starting server on 4567")

  sys addShutdownHook {
    println("Scalatra bids you farewell")
  }

  server.start()
  server.join()
}

get("/") {
  "Hello, World!!!"
}

get("/:thing") {
  "Hello %s" format params('thing)
}

name := "scalatra-host"

version := "0.0.1-SNAPSHOT"

organization := "org.scalatra"

scalaVersion := "2.9.1"

scalacOptions ++= Seq("-optimize", "-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")

libraryDependencies ++= Seq(
  "org.scalatra"          %% "scalatra" % "2.1.0-SNAPSHOT",
  "org.eclipse.jetty"     % "jetty-webapp" % "8.0.1.v20110908",
  "org.eclipse.jetty"     % "jetty-server" % "8.0.1.v20110908",
  "org.scala-lang"        % "scala-compiler" % "2.9.1"
)

resolvers += "Sonatype Nexus Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

mainClass := Some("org.scalatra.ScalatraMain") 

Run a scalatra script file

git clone git://gist.github.com/1208309.git scalatra_scripting

xsbt 'run hello_world'