Skip to content

Instantly share code, notes, and snippets.

@muuki88
Last active Oct 30, 2016
Embed
What would you like to do?
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