Service.become[Http]("http-echo", 9000){ case request @ Get on Root => request.ok("Hello world!") case request @ Get on Root / "echo" / str => request.ok(str) } }
KeyValExample
classKeyValDBextendsActor{
importKeyValDB._
val db = collection.mutable.Map[ByteString, ByteString]()
/** * Here we're demonstrating a common way of breaking out the business logic * from the server setup, which makes functional testing easy */ classHttpRoutes(redis: LocalClient[Command, Reply]) { definvalidReply(reply: Reply) = s"Invalid reply from redis $reply"
val handler: PartialFunction[HttpRequest, Response[HttpResponse]] = { case req @ Get on Root => req.ok("Hello World!")
case req @ Get on Root / "get" / key => redis.send(Commands.Get(ByteString(key))).map{ caseBulkReply(data) => req.ok(data.utf8String) caseNilReply => req.notFound("(nil)") case other => req.error(invalidReply(other)) }
case req @ Get on Root / "set" / key / value => redis.send(Commands.Set(ByteString(key), ByteString(value))).map{ caseStatusReply(msg) => req.ok(msg) case other => req.error(invalidReply(other)) }
}
}
defstart(port: Int, redisAddress: InetSocketAddress)(implicit system: IOSystem): ServerRef = { Service.serve[Http]("http-example", port){context => val redis = context.clientFor[Redis](redisAddress.getHostName, redisAddress.getPort) //because our routes object has no internal state, we can share it among //connections. If the class did have some per-connection internal state, //we'd just create one per connection val routes = newHttpRoutes(redis)