Skip to content

Instantly share code, notes, and snippets.

@mkf-simpson
Created February 13, 2019 13:19
Show Gist options
  • Save mkf-simpson/5b39f0572431cf6e4cd65da16a037b10 to your computer and use it in GitHub Desktop.
Save mkf-simpson/5b39f0572431cf6e4cd65da16a037b10 to your computer and use it in GitHub Desktop.
Finch with FS2
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)
}
@mkf-simpson
Copy link
Author

curl -D - -X POST -H "Transfer-Encoding: chunked" -H "Content-Type: text/plain" --data-binary "abcdef" 
http://localhost:8086/streaming

HTTP/1.1 404 Not Found
Date: Wed, 13 Feb 2019 13:10:57 GMT
Server: Finch
Content-Length: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment