Skip to content

Instantly share code, notes, and snippets.

@caniszczyk
Last active August 29, 2015 13:56
Show Gist options
  • Save caniszczyk/8921257 to your computer and use it in GitHub Desktop.
Save caniszczyk/8921257 to your computer and use it in GitHub Desktop.
Composing Services via Finagle
val timelineSvc = Thrift.newIface[TimelineService](...) // #1
val tweetSvc = Thrift.newIface[TweetService](...)
val authSvc = Thrift.newIface[AuthService](...)
val authFilter = Filter.mk[Req, AuthReq, Res, Res] { (req, svc) => // #2
authSvc.authenticate(req) flatMap svc(_)
}
val apiService = Service.mk[AuthReq, Res] { req =>
timelineSvc(req.userId) flatMap {tl =>
val tweets = tl map tweetSvc.getById(_)
Future.collect(tweets) map tweetsToJson(_) }
}
} //#3
Http.serve(":80", authFilter andThen apiService) // #4
// #1 Create a client for each service
// #2 Create new Filter to authenticate incoming requests
// #3 Create a service to convert an authenticated timeline request to a json response
// #4 Start a new HTTP server on port 80 using the authenticating filter and our service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment