timt / scala-embedded-jetty   32

GitHub

Simple scala shim for configuring and running embedded jetty

Scala versions: 2.12 2.11 2.10

scala-embedded-jetty Build Status Download

A scala shim for running embedded jetty (http only currently)

Requirements

Usage

Add the following lines to your build.sbt

resolvers += "Tim Tennant's repo" at "http://dl.bintray.com/timt/repo/"

val jettyVersion="9.2.10.v20150310"

libraryDependencies ++= Seq(
  "io.shaka"            %%  "scala-embedded-jetty"  % "28"
  "io.shaka"            %%  "naive-http"            % "78",
  "org.eclipse.jetty"   %   "jetty-webapp"          % jettyVersion,
  "org.eclipse.jetty"   %   "jetty-plus"            % jettyVersion,
  "org.eclipse.jetty"   %   "jetty-servlets"        % jettyVersion,

Starting a server

import io.shaka.jetty.EmbeddedJetty
val embeddedJetty = jetty.start()  //Start on some free port
...
val embeddedJetty = jetty(1234).start() //Start on port 1234

Customise configuration

import io.shaka.jetty.JettyConfiguration
val jettyConfiguration = JettyConfiguration(port = 1234, logsDirectory = "src/resource/logs")
val embeddedJetty = jetty(jettyConfiguration).start()

Stopping the server

embeddedJetty.stop()

Providing a custom context handler

io.shaka.http.Response.respond
io.shaka.http.Request
jetty
    .addHandler("/my-context", (request: Request) => {
      respond("<h1>Hello World</h1>".getBytes)
    })
    .start()

For more examples see

Code license

Apache License 2.0