git clone git://gist.github.com/1208309.git scalatra_scripting
xsbt 'run hello_world'
Created
September 10, 2011 13:30
-
-
Save casualjim/1208309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get("/") { | |
| "Hello, World!!!" | |
| } | |
| get("/:thing") { | |
| "Hello %s" format params('thing) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment