Browse Source

Merge branch 'update-to-new-spray-version' of https://github.com/jrudolph/FrameworkBenchmarks into jrudolph-update-to-new-spray-version

Patrick Falls 12 years ago
parent
commit
ae1a0c23a6

+ 1 - 0
spray/benchmark_config

@@ -4,6 +4,7 @@
         "default" : {
             "setup_file" : "setup",
             "json_url" : "/json",
+            "plaintext_url": "/plaintext",
             "port": 8080,
             "sort": 114
             }

+ 1 - 1
spray/build.sbt

@@ -15,7 +15,7 @@ resolvers ++= Seq(
 
 libraryDependencies ++= Seq(
   "io.spray" %% "spray-json" % "1.2.4",
-  "io.spray" % "spray-can" % "1.1-20130513",
+  "io.spray" % "spray-can" % "1.1-M8",
   "com.typesafe.akka" %%  "akka-actor" % "2.1.2",
   "com.typesafe.akka" %%  "akka-slf4j" % "2.1.2",
   "ch.qos.logback"% "logback-classic" % "1.0.12" % "runtime"

+ 16 - 8
spray/src/main/scala/spray/examples/BenchmarkService.scala

@@ -5,6 +5,7 @@ import scala.concurrent.duration._
 import spray.can.Http
 import spray.json._
 import spray.http._
+import spray.util._
 import MediaTypes._
 import HttpMethods._
 import StatusCodes._
@@ -13,11 +14,21 @@ class BenchmarkService extends Actor {
   import context.dispatcher
   import Uri._
   import Uri.Path._
+  val message = "Hello, World!".getAsciiBytes
+  val unknownResource = HttpResponse(NotFound, entity = "Unknown resource!")
 
   def fastPath: Http.FastPath = {
-    case HttpRequest(GET, Uri(_, _, Slash(Segment("json", Path.Empty)), _, _), _, _, _) =>
-      val json = JsObject("message" -> JsString("Hello, World!"))
-      HttpResponse(entity = HttpEntity(ContentType.`application/json`, json.compactPrint))
+    case HttpRequest(GET, Uri(_, _, Slash(Segment(x, Path.Empty)), _, _), _, _, _) =>
+      x match {
+        case "json" =>
+          val json = JsObject("message" -> JsString("Hello, World!"))
+          HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, json.compactPrint))
+        case "plaintext" => HttpResponse(entity = HttpEntity(ContentTypes.`text/plain`, message))
+        case "stop" =>
+          context.system.scheduler.scheduleOnce(1.second) { context.system.shutdown() }
+          HttpResponse(entity = "Shutting down in 1 second ...")
+        case _ => unknownResource
+      }
   }
 
   def receive = {
@@ -32,6 +43,7 @@ class BenchmarkService extends Actor {
             <p>Defined resources:</p>
             <ul>
               <li><a href="/json">/json</a></li>
+              <li><a href="/plaintext">/plaintext</a></li>
               <li><a href="/stop">/stop</a></li>
             </ul>
           </body>
@@ -39,10 +51,6 @@ class BenchmarkService extends Actor {
       )
     )
 
-    case HttpRequest(GET, Path("/stop"), _, _, _) =>
-      sender ! HttpResponse(entity = "Shutting down in 1 second ...")
-      context.system.scheduler.scheduleOnce(1.second) { context.system.shutdown() }
-
-    case _: HttpRequest => sender ! HttpResponse(NotFound, entity = "Unknown resource!")
+    case _: HttpRequest => sender ! unknownResource
   }
 }