Last active
October 30, 2016 10:27
-
-
Save muuki88/97df1305b4cee51713b537575aadddf7 to your computer and use it in GitHub Desktop.
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
| package net.gutefrage | |
| import com.twitter.finagle._ | |
| import com.twitter.finagle.http.service.HttpResponseClassifier | |
| import com.twitter.server.TwitterServer | |
| import com.twitter.util.Await | |
| import io.finch._ | |
| import io.finch.circe._ | |
| import io.circe.generic.auto._ | |
| import net.gutefrage.temperature.thrift._ | |
| /** | |
| * Serves the mean temperature at `/weather/mean` | |
| */ | |
| object WeatherApi extends TwitterServer { | |
| def main(): Unit = { | |
| val client = ThriftMux.client.newIface[TemperatureService.FutureIface]( | |
| "zk!127.0.0.1:2181!/service/temperature", "weather-api-client") | |
| /** json model */ | |
| case class Mean(mean: Double) | |
| // mean temperature endpoint | |
| val api: Endpoint[Mean] = get("weather" / "mean") { | |
| client.mean().map(mean => Ok(Mean(mean))) | |
| } | |
| // start and announce the server | |
| val server = Http.server | |
| .withLabel("weather-api") | |
| // 5xx response should be classified as server errors | |
| .withResponseClassifier(HttpResponseClassifier.ServerErrorsAsFailures) | |
| .serveAndAnnounce( | |
| name = "zk!127.0.0.1:2181/service/weather!0", | |
| addr = s":8080", | |
| service = api.toService | |
| ) | |
| closeOnExit(server) | |
| Await.ready(server) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment