Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 34 additions & 24 deletions
58
src/main/scala/me/eax/finagle_example/FinagleServiceExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,58 @@ | ||
package me.eax.finagle_example | ||
|
||
import me.eax.finagle_example.services._ | ||
import me.eax.finagle_example.utils._ | ||
|
||
import com.escalatesoft.subcut.inject.{Injectable, BindingModule} | ||
import com.twitter.finagle.Service | ||
import com.twitter.finagle.http.Response | ||
import com.twitter.util.Future | ||
import com.twitter.util.{Future => TwitterFuture} | ||
import org.jboss.netty.handler.codec.http._ | ||
import org.jboss.netty.util.CharsetUtil | ||
|
||
import com.typesafe.scalalogging._ | ||
import org.slf4j.LoggerFactory | ||
|
||
import scala.collection.concurrent.TrieMap | ||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class FinagleServiceExample(implicit val bindingModule: BindingModule) | ||
extends Service[HttpRequest, HttpResponse] with Injectable { | ||
|
||
class FinagleServiceExample extends Service[HttpRequest, HttpResponse] { | ||
private val kv = TrieMap.empty[String, String] | ||
private val kv = inject[KeyValueStorage] | ||
private val logger = Logger(LoggerFactory.getLogger(this.getClass)) | ||
|
||
logger.info(s"service started") | ||
|
||
def apply(req: HttpRequest): Future[HttpResponse] = { | ||
Future { | ||
val resp = Response(req.getProtocolVersion, HttpResponseStatus.OK) | ||
val key = req.getUri | ||
def apply(req: HttpRequest): TwitterFuture[HttpResponse] = { | ||
val resp = Response(req.getProtocolVersion, HttpResponseStatus.OK) | ||
val key = req.getUri | ||
|
||
req.getMethod match { | ||
case HttpMethod.GET => | ||
logger.debug(s"reading $key") | ||
kv.get(key) match { | ||
req.getMethod match { | ||
case HttpMethod.GET => | ||
logger.debug(s"reading $key") | ||
for { | ||
optValue <- kv.get(key) | ||
} yield { | ||
optValue match { | ||
case None => | ||
resp.setStatus(HttpResponseStatus.NOT_FOUND) | ||
case Some(value) => | ||
resp.setContentString(value) | ||
} | ||
case HttpMethod.POST => | ||
logger.debug(s"writing $key") | ||
val value = req.getContent.toString(CharsetUtil.UTF_8) | ||
kv.update(key, value) | ||
case HttpMethod.DELETE => | ||
logger.debug(s"deleting $key") | ||
kv.remove(key) | ||
case _ => | ||
logger.error(s"bad request: $req") | ||
resp.setStatus(HttpResponseStatus.BAD_REQUEST) | ||
} | ||
resp | ||
resp | ||
} | ||
case HttpMethod.POST => | ||
logger.debug(s"writing $key") | ||
val value = req.getContent.toString(CharsetUtil.UTF_8) | ||
kv.update(key, value).map(_ => resp) | ||
case HttpMethod.DELETE => | ||
logger.debug(s"deleting $key") | ||
kv.remove(key).map(_ => resp) | ||
case _ => | ||
logger.error(s"bad request: $req") | ||
resp.setStatus(HttpResponseStatus.BAD_REQUEST) | ||
Future.successful(resp) | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/scala/me/eax/finagle_example/services/KeyValueStorage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.eax.finagle_example.services | ||
|
||
import scala.collection.concurrent.TrieMap | ||
import scala.concurrent._ | ||
|
||
trait KeyValueStorage { | ||
def get(key: String): Future[Option[String]] | ||
def update(key: String, value: String): Future[Unit] | ||
def remove(key: String): Future[Unit] | ||
} | ||
|
||
class KeyValueStorageImpl extends KeyValueStorage { | ||
private val kv = TrieMap.empty[String, String] | ||
|
||
def get(key: String): Future[Option[String]] = { | ||
val result = kv.get(key) | ||
Future.successful(result) | ||
} | ||
|
||
def update(key: String, value: String): Future[Unit] = { | ||
val result = kv.update(key, value) | ||
Future.successful(result) | ||
} | ||
|
||
def remove(key: String): Future[Unit] = { | ||
kv.remove(key) | ||
Future.successful({}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.eax.finagle_example | ||
|
||
import scala.util._ | ||
import scala.concurrent._ | ||
|
||
package object utils { | ||
implicit def scalaToTwitterTry[T](t: Try[T]): com.twitter.util.Try[T] = t match { | ||
case Success(r) => com.twitter.util.Return(r) | ||
case Failure(ex) => com.twitter.util.Throw(ex) | ||
} | ||
|
||
implicit def twitterToScalaTry[T](t: com.twitter.util.Try[T]): Try[T] = t match { | ||
case com.twitter.util.Return(r) => Success(r) | ||
case com.twitter.util.Throw(ex) => Failure(ex) | ||
} | ||
|
||
implicit def scalaToTwitterFuture[T](f: Future[T])(implicit ec: ExecutionContext): com.twitter.util.Future[T] = { | ||
val promise = com.twitter.util.Promise[T]() | ||
f.onComplete(promise update _) | ||
promise | ||
} | ||
|
||
implicit def twitterToScalaFuture[T](f: com.twitter.util.Future[T]): Future[T] = { | ||
val promise = Promise[T]() | ||
f.respond(promise complete _) | ||
promise.future | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters