Last active
September 7, 2015 04:16
-
-
Save jfernandez/61675081ca8fec3eb171 to your computer and use it in GitHub Desktop.
Finagle: serve Thrift over HTTP
This file contains hidden or 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
object Main extends App { | |
val filter = new FooFilter | |
val fooService = new FooService.FinagledService(new FooServiceImpl, new TCompactProtocol.Factory()) // Use the protocol your client uses | |
val httpService: Service[HttpRequest, HttpResponse] = filter andThen fooService | |
val server = Http.serve(":10051", httpService) | |
Await.ready(server) | |
} | |
class FooFilter extends Filter[HttpRequest, HttpResponse, Array[Byte], Array[Byte]] { | |
override def apply(request: HttpRequest, service: Service[Array[Byte], Array[Byte]]): Future[HttpResponse] = { | |
val thriftFuture = service(request.getContent.array()) | |
thriftFuture flatMap { res => | |
val response = new DefaultHttpResponse(request.getProtocolVersion, HttpResponseStatus.OK) | |
response.setContent(ChannelBuffers.wrappedBuffer(res)) | |
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.getContent.readableBytes.toString) | |
Future.value(response) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment