Created
February 13, 2019 13:19
-
-
Save kmakarychev-dev/5b39f0572431cf6e4cd65da16a037b10 to your computer and use it in GitHub Desktop.
Finch with FS2
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 finchtest | |
| import cats.effect._ | |
| import cats.implicits._ | |
| import com.twitter.finagle.Http | |
| import com.twitter.util | |
| import fs2.Stream | |
| import io.finch._ | |
| import io.finch.fs2._ | |
| class HttpService[F[_]: Effect] extends Endpoint.Module[F] { | |
| def streaming = post("streaming" :: stringBodyStream[Stream]) { stream: Stream[F, String] => | |
| Ok("ok").pure[F] | |
| } | |
| def api = streaming.toServiceAs[Text.Plain] | |
| } | |
| object Main extends IOApp { | |
| def whenTerminated[F[_]](implicit A: Async[F]): F[Unit] = { | |
| A.async(cb => { | |
| sys.addShutdownHook(cb(Right(()))) | |
| }) | |
| } | |
| override def run(args: List[String]): IO[ExitCode] = for { | |
| http <- IO(Http.serve(s"0.0.0.0:8086", new HttpService[IO].api)) | |
| stop <- IO.race(whenTerminated[IO], IO(util.Await.ready(http))) | |
| code <- stop match { | |
| case Left(_) => IO(http.close(util.Duration.Top)).map(_ => 0) | |
| case Right(_) => IO.pure(1) | |
| } | |
| } yield ExitCode(code) | |
| } |
Author
kmakarychev-dev
commented
Feb 13, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment