Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
package com.example
import spray.routing._
import spray.http._
import MediaTypes._
// we don't implement our route structure directly in the service actor because
// we want to be able to test it independently, without having to spin up an actor
class MyServiceActor extends Actor with MyService
// the HttpService trait defines only one abstract member, which
// connects the services environment to the enclosing actor or test
def actorRefFactory = context
// this actor only runs our route, but you could add
// other things here, like request stream processing
// or timeout handling
def receive = runRoute(myRoute)
// this trait defines our service behavior independently from the service actor
trait MyService extends HttpService
val myRoute =
path("") do
get do
respondWithMediaType(`text/html`) do // XML is marshalled to `text/xml` by default, so we simply override here
complete do
<h1>Say hello to <i>spray-routing</i> on <i>spray-can</i>!</h1>
package com.example
import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http._
import StatusCodes._
class MyServiceSpec extends Specification with Specs2RouteTest with MyService
def actorRefFactory = system
"MyService" should do
"return a greeting for GET requests to the root path" in do
Get() ~> myRoute ~> check do
responseAs[String] must contain("Say hello")
"leave GET requests to other paths unhandled" in do
Get("/kermit") ~> myRoute ~> check do
handled must beFalse
"return a MethodNotAllowed error for PUT requests to the root path" in do
Put() ~> sealRoute(myRoute) ~> check do
status === MethodNotAllowed
responseAs[String] === "HTTP method not allowed, supported methods: GET"
package example
import scala.scalajs.js.annotation.JSExport
import org.scalajs.dom
import scala.util.Random
case class Point(x: Int, y: Int) do
def +(p: Point) = Point(x + p.x, y + p.y)
def /(d: Int) = Point(x / d, y / d)
object ScalaJSExample
def main(canvas: dom.HTMLCanvasElement): Unit =
val ctx = canvas.getContext("2d")
var count = 0
var p = Point(0, 0)
val corners = Seq(Point(255, 255), Point(0, 255), Point(128, 0))
def clear() =
ctx.fillStyle = "black"
ctx.fillRect(0, 0, 255, 255)
def run = for (i <- 0 until 10)
if (count % 3000 == 0) clear()
count += 1
p = (p + corners(Random.nextInt(3))) / 2
val height = 512.0 / (255 + p.y)
val r = (p.x * height).toInt
val g = ((255-p.x) * height).toInt
val b = p.y
ctx.fillStyle = s"rgb($g, $r, $b)"
ctx.fillRect(p.x, p.y, 1, 1)
dom.setInterval(() => run, 50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment