Browse Source

spray: implement `/plaintext` handler

Johannes Rudolph 12 years ago
parent
commit
5c6eb3ef1c
1 changed files with 11 additions and 3 deletions
  1. 11 3
      spray/src/main/scala/spray/examples/BenchmarkService.scala

+ 11 - 3
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,17 @@ class BenchmarkService extends Actor {
   import context.dispatcher
   import Uri._
   import Uri.Path._
+  val message = "Hello, World!".getAsciiBytes
 
   def fastPath: Http.FastPath = {
-    case HttpRequest(GET, Uri(_, _, Slash(Segment("json", Path.Empty)), _, _), _, _, _) =>
-      val json = JsObject("message" -> JsString("Hello, World!"))
-      HttpResponse(entity = HttpEntity(ContentTypes.`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))
+      }
   }
 
   def receive = {
@@ -32,6 +39,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>