Skip to content

Commit 87abcb9

Browse files
committed
spray: implement /plaintext handler
1 parent c954ec0 commit 87abcb9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

spray/src/main/scala/spray/examples/BenchmarkService.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scala.concurrent.duration._
55
import spray.can.Http
66
import spray.json._
77
import spray.http._
8+
import spray.util._
89
import MediaTypes._
910
import HttpMethods._
1011
import StatusCodes._
@@ -13,11 +14,17 @@ class BenchmarkService extends Actor {
1314
import context.dispatcher
1415
import Uri._
1516
import Uri.Path._
17+
val message = "Hello, World!".getAsciiBytes
1618

1719
def fastPath: Http.FastPath = {
18-
case HttpRequest(GET, Uri(_, _, Slash(Segment("json", Path.Empty)), _, _), _, _, _) =>
19-
val json = JsObject("message" -> JsString("Hello, World!"))
20-
HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, json.compactPrint))
20+
case HttpRequest(GET, Uri(_, _, Slash(Segment(x, Path.Empty)), _, _), _, _, _) =>
21+
x match {
22+
case "json" =>
23+
val json = JsObject("message" -> JsString("Hello, World!"))
24+
HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, json.compactPrint))
25+
case "plaintext" =>
26+
HttpResponse(entity = HttpEntity(ContentTypes.`text/plain`, message))
27+
}
2128
}
2229

2330
def receive = {
@@ -32,6 +39,7 @@ class BenchmarkService extends Actor {
3239
<p>Defined resources:</p>
3340
<ul>
3441
<li><a href="/json">/json</a></li>
42+
<li><a href="/plaintext">/plaintext</a></li>
3543
<li><a href="/stop">/stop</a></li>
3644
</ul>
3745
</body>

0 commit comments

Comments
 (0)