Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Last active December 19, 2015 20:58
Show Gist options
  • Select an option

  • Save nicerobot/6016414 to your computer and use it in GitHub Desktop.

Select an option

Save nicerobot/6016414 to your computer and use it in GitHub Desktop.
//
package controllers
import play.api.mvc.Action
import play.api.mvc.Controller
import play.api.libs.ws.WS
import scala.concurrent.Future
import play.api.libs.json.Json
import play.api.libs.json.JsValue
import play.api.libs.concurrent.Execution.Implicits._
object Application extends Controller {
def timed(url: String):Future[Timing] = {
val start = System.currentTimeMillis()
WS.url(url).get().map(_ => Timing(url, System.currentTimeMillis() - start))
}
case class Timing(url: String, latency: Long)
implicit val hogeFormat = Json.format[Timing]
def index = Action {
val yahoo = timed("http://www.yahoo.com")
val google = timed("http://www.google.com")
val bing = timed("http://www.bing.com")
val all = Future.sequence(Seq(yahoo, google, bing))
Async {
all.map(timings => Ok(Json.toJson(timings)))
}
}
}
#!/bin/bash
which play >/dev/null || {
[ -f play-2.1.2.zip ] || curl -O http://downloads.typesafe.com/play/2.1.2/play-2.1.2.zip
[ -d play-2.1.2 ] || unzip play-2.1.2.zip >/dev/null 2>&1 || exit ${LINENO}
export PATH=${PWD}/play-2.1.2:${PATH}
}
which play >/dev/null || exit ${LINENO}
[ -d asyncio ] || play new asyncio<<<"$(printf '\n1\n')" || exit ${LINENO}
cd asyncio || exit ${LINENO}
(
cd app/controllers && curl -ks -o Application.scala https://gist.github.com/nicerobot/6016414/raw/play-121.scala
) || exit ${LINENO}
play run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment