Skip to content

Instantly share code, notes, and snippets.

@dpsoft
Created February 27, 2018 01:05
Show Gist options
  • Save dpsoft/79f48c9224d0dd55fed8ad3d2ec80ba0 to your computer and use it in GitHub Desktop.
Save dpsoft/79f48c9224d0dd55fed8ad3d2ec80ba0 to your computer and use it in GitHub Desktop.
object GoogleService {
def service[F[_]: Effect](c: Client[F]): HttpService[F] = {
val dsl = new Http4sDsl[F]{}
import dsl._
HttpService[F] {
case GET -> Root / "not" =>
Thread.sleep((Math.random() * 1000).toLong)
NotFound("NotFound")
case req@GET -> Root / "call-google" =>
println(req.headers)
Ok(c.expect[String]("https://www.google.com.ar"))
}
}
}
object Server extends StreamApp[IO] {
Kamon.addReporter(new PrometheusReporter())
Kamon.addReporter(new ZipkinReporter())
def serve[F[_]](implicit Effect: Effect[F], EC: ExecutionContext) : Stream[F, StreamApp.ExitCode] =
for {
_ <- Stream.eval(Sync[F].delay(println("Starting Google Service with Client")))
client <- Http1Client.stream[F]()
service = GoogleService.service[F](middleware.client.KamonSupport(client))
exitCode <- BlazeBuilder[F]
.bindHttp(Config.server.port, Config.server.interface)
.mountService(middleware.server.KamonSupport(service))
.serve
} yield exitCode
override def stream(args: List[String], requestShutdown: IO[Unit]): Stream[IO, StreamApp.ExitCode] = {
implicit val executor: ExecutionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(Config.server.threadPool.size))
serve[IO]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment