Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created October 13, 2011 02:49
Show Gist options
  • Save xuwei-k/1283212 to your computer and use it in GitHub Desktop.
Save xuwei-k/1283212 to your computer and use it in GitHub Desktop.
scalatra embedded jetty
scalaVersion := "2.9.1"
libraryDependencies ++= {
val jettyVersion = "7.2.2.v20101205"
Seq(
"org.eclipse.jetty" % "jetty-webapp" % jettyVersion
,"org.eclipse.jetty" % "jetty-continuation" % "7.5.1.v20110908" % "compile"
,"org.scalatra" %% "scalatra" % "2.0.1"
)
}
package scalatra_embedded_jetty
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.handler.{
ContextHandlerCollection
}
import org.eclipse.jetty.server.bio.SocketConnector
import org.eclipse.jetty.servlet.{
FilterHolder, FilterMapping, ServletContextHandler, ServletHolder
}
import javax.servlet.{
Filter,FilterChain,FilterConfig,ServletRequest,ServletResponse
}
import org.scalatra.ScalatraFilter
object Main{
def main(args: Array[String]){
val s = S("0.0.0.0",5678,F)
s.run
Console.readLine
s.stop
}
}
object F extends ScalatraFilter{
get("/*"){
<h1>hello</h1>
}
get("/foo"){
<h1>bar</h1>
}
}
case class S(host:String,port:Int,filt:Filter){
val server = new Server()
val handlers = new ContextHandlerCollection
val conn= new SocketConnector()
val current = contextHandler("/")
private def contextHandler(path: String) = {
val ctx = new ServletContextHandler(handlers, path, false, false)
val holder = new ServletHolder(classOf[org.eclipse.jetty.servlet.DefaultServlet])
ctx.addServlet(holder, "/")
handlers.addHandler(ctx)
ctx
}
filter(filt)
def filter(filt: Filter){
val holder = new FilterHolder(filt)
current.addFilter(holder, "/*", FilterMapping.DEFAULT)
}
def run(){
val holder = new FilterHolder(filt)
server.setHandler(handlers)
server.setStopAtShutdown(true)
conn.setPort(port)
conn.setHost(host)
server.addConnector(conn)
server.start()
}
def stop(){
server.stop()
}
}
@xuwei-k
Copy link
Author

xuwei-k commented Nov 27, 2011

Of cause I already know that if using unfiltered , code is simply.

I wrote this code with reference unfiltered jetty module :)

https://github.com/unfiltered/unfiltered/tree/0.5.1/jetty/src/main/scala

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment