Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A server is just a function: introduction to ht...

A server is just a function: introduction to http4s

If you're going to write a modern web service, you have a wide range of frameworks and libraries to choose from. One of them is http4s, a functional library for writing HTTP services and clients in Scala. I'm going to show you why you should consider it for your next application, for reasons including but not limited to:
- type-safe request/response models that are uniform between client and server
- resource-safe servers, response/request bodies, and streaming
- framework-agnostic testing.

At the end of this talk, you should have a clear understanding of how http4s can make it easier to build modern, runtime-safe services in Scala.

Jakub Kozłowski

April 25, 2019
Tweet

More Decks by Jakub Kozłowski

Other Decks in Programming

Transcript

  1. A SERVER IS JUST A FUNCTION A N I N

    T R O D U C T I O N TO H T T P 4 S J A K U B KO Z Ł O W S K I
  2. HTTP4S Purely functional, streaming HTTP server/client Multiple backends Built on

    cats-effect and fs2 Supports websockets (server) https://http4s.org
  3. KLEISLI val len: Kleisli[IO, Token, Int] = Kleisli { token

    => IO.pure(token.length) } val cloned: Kleisli[IO, Token, String] = Kleisli { token => IO.pure(token + token) }
  4. KLEISLI val f3 = for { a <- len b

    <- cloned } yield (a + b) val f4 = (len, cloned).tupled val len: Kleisli[IO, Token, Int] = Kleisli { token => IO.pure(token.length) } val cloned: Kleisli[IO, Token, String] = Kleisli { token => IO.pure(token + token) }
  5. OPTIONT val num: OptionT[IO, Int] = OptionT(IO.pure(42.some)) def text(num: Int):

    OptionT[IO, String] = num match { case n if n % 2 === 0 => OptionT(IO.pure("foo".some)) case _ => OptionT.none }
  6. OPTIONT val num: OptionT[IO, Int] = OptionT(IO.pure(42.some)) def text(num: Int):

    OptionT[IO, String] = num match { case n if n % 2 === 0 => OptionT(IO.pure("foo".some)) case _ => OptionT.none } val result = for { n <- num s <- text(n) s2 <- text(n + 1) s3 <- text(n + 2) } yield s + s2 + s3 val unwrapped: IO[Option[String]] = result.value
  7. HTTP4S SERVER: THE REAL REAL THING HttpApp[F[_]] = Kleisli[F, Request[F],

    Response[F]] HttpRoutes[F[_]] = Kleisli[OptionT[F, ?], Request[F], Response[F]] Effect type of entity body stream
  8. HTTP4S SERVER: THE REAL REAL THING Http[F[_], G[_]] = Kleisli[F,

    Request[G], Response[G]] HttpApp[F[_]] = Http[F, F] HttpRoutes[F[_]] = Http[OptionT[F, ?], F]
  9. object Main extends IOApp { val routes = HttpRoutes.of[IO] {

    case GET -> Root / "hello" => Ok("Hello, world!") } def run(args: List[String]): IO[ExitCode] = BlazeServerBuilder[IO] .withHttpApp(routes.orNotFound) .bindHttp(port = 8080) .resource .use(_ => IO.never) }
  10. object Main extends IOApp { val routes = HttpRoutes.of[IO] {

    case GET -> Root / "hello" => Ok("Hello, world!") } def run(args: List[String]): IO[ExitCode] = BlazeServerBuilder[IO] .withHttpApp( ) .bindHttp(port = 8080) .resource .use(_ => IO.never) } routes.orNotFound HttpRoutes => HttpApp
  11. object Main extends IOApp { val routes = HttpRoutes.of[IO] {

    case GET -> Root / "hello" => Ok("Hello, world!") } def run(args: List[String]): IO[ExitCode] = BlazeServerBuilder[IO] .withHttpApp(routes.orNotFound) .bindHttp(port = 8080) .resource .use(_ => IO.never) }
  12. HttpRoutes.of[IO] { case request @ POST -> Root / "echo"

    => Ok( ) } request.body Literally Stream[IO, Byte]
  13. val request = Request[IO](uri = uri"https://http4s.org") val remoteCall: IO[String] =

    client.expect[String](request) remoteCall BlazeClientBuilder[IO](executionContext).resource.use { client => }
  14. case GET -> Root / "remote" / "stream" => val

    response = client.stream(request).flatMap(_.body) Ok(response)
  15. trait Client[F[_]] { def run(req: Request[F]): Resource[F, Response[F]] } def

    apply[F[_]](f: Request[F] => Resource[F, Response[F]] ): Client[F]
  16. trait Client[F[_]] { def run(req: Request[F]): Resource[F, Response[F]] } def

    apply[F[_]](f: Request[F] => Resource[F, Response[F]] ): Client[F]
  17. trait Client[F[_]] { def run(req: Request[F]): Resource[F, Response[F]] } def

    fromHttpApp[F[_]](app: HttpApp[F]): Client[F] def apply[F[_]](f: Request[F] => Resource[F, Response[F]] ): Client[F]
  18. T E S T I N G R E S

    O U R C E S A F E T Y E X T E N S I B I L I T Y
  19. T E S T I N G R E S

    O U R C E S A F E T Y E X T E N S I B I L I T Y
  20. TESTING ROUTES def routes(client: Client[IO]): HttpRoutes[IO] = { HttpRoutes.of[IO] {

    //... case GET -> Root / "remote" => val remoteCall: IO[String] = client.expect[String](request) for { result <- remoteCall response <- Ok(result) } yield response //... } }
  21. val body = Json.obj("foo" -> Json.fromString("bar")) val request = Request[IO](method

    = Method.POST, uri = uri"/echo").withEntity(body) val remoteClient = Client.fromHttpApp(HttpApp.notFound[IO]) val routes = Main.routes(remoteClient) httsps://http4s.org TESTING ROUTES
  22. routes.run(request).value.flatMap(_.value.as[Json]).map(_ shouldBe body) val remoteClient = Client.fromHttpApp(HttpApp.notFound[IO]) val routes =

    Main.routes(remoteClient) val body = Json.obj("foo" -> Json.fromString("bar")) val request = Request[IO](method = Method.POST, uri = uri"/echo").withEntity(body) TESTING ROUTES
  23. routes.run(request).value.flatMap(_.value.as[Json]).map(_ shouldBe body) val remoteClient = Client.fromHttpApp(HttpApp.notFound[IO]) val routes =

    Main.routes(remoteClient) val body = Json.obj("foo" -> Json.fromString("bar")) val request = Request[IO](method = Method.POST, uri = uri"/echo").withEntity(body) TESTING ROUTES Client.fromHttpApp(routes.orNotFound).expect[Json](request).map(_ shouldBe body) or
  24. TESTING HTTP val blazeClient = BlazeClientBuilder[IO](ExecutionContext.global).resource Main.server.use { server =>

    blazeClient.use { client => } } client.expect[String](server.baseUri / "hello") .map(_ shouldBe "Hello world!")
  25. TESTING CLIENTS class TodoClient(client: Client[IO]) { def getTodo(id: Int): IO[Todo]

    = client.expect[Todo](Request[IO](uri = uri"/todos" / id.toString)) }
  26. TESTING CLIENTS class TodoClient(client: Client[IO]) { def getTodo(id: Int): IO[Todo]

    = client.expect[Todo](Request[IO](uri = uri"/todos" / id.toString)) } //in test val mockServer = HttpRoutes .of[IO] { case GET -> Root / "todos" / IntVar(id) => Ok(Todo(id, false)) } .orNotFound
  27. TESTING CLIENTS val mockServer = HttpRoutes .of[IO] { case GET

    -> Root / "todos" / IntVar(id) => Ok(Todo(id, false)) } .orNotFound
  28. TESTING CLIENTS val clientRaw = Client.fromHttpApp(mockServer) val todoClient = new

    TodoClient(clientRaw) val mockServer = HttpRoutes .of[IO] { case GET -> Root / "todos" / IntVar(id) => Ok(Todo(id, false)) } .orNotFound
  29. TESTING CLIENTS val mockServer = HttpRoutes .of[IO] { case GET

    -> Root / "todos" / IntVar(id) => Ok(Todo(id, false)) } .orNotFound val clientRaw = Client.fromHttpApp(mockServer) val todoClient = new TodoClient(clientRaw) todoClient.getTodo(1).map(_ shouldBe Todo(1, false))
  30. T E S T I N G R E S

    O U R C E S A F E T Y E X T E N S I B I L I T Y
  31. T E S T I N G R E S

    O U R C E S A F E T Y E X T E N S I B I L I T Y
  32. RESOURCE abstract class Resource[F[_], A] { def use[B](f: A =>

    F[B]): F[B] } object Resource { def make[F[_], A](acquire: F[A]) (release: A => F[Unit]): Resource[F, A] }
  33. RESOURCE val server = for { db <- Resource.make(IO(createPool()))(pool =>

    IO(pool.close())) server <- BlazeServerBuilder[IO].withHttpApp(routes(db)) } yield server server.use(_ => IO.sleep(10.seconds))
  34. RESOURCE val server = for { db <- Resource.make(IO(createPool()))(pool =>

    IO(pool.close())) server <- BlazeServerBuilder[IO].withHttpApp(routes(db)) } yield server server.use(_ => IO.sleep(10.seconds)) 1. Create pool
  35. RESOURCE val server = for { db <- Resource.make(IO(createPool()))(pool =>

    IO(pool.close())) server <- BlazeServerBuilder[IO].withHttpApp(routes(db)) } yield server server.use(_ => IO.sleep(10.seconds)) 1. Create pool 2. Start server
  36. RESOURCE val server = for { db <- Resource.make(IO(createPool()))(pool =>

    IO(pool.close())) server <- BlazeServerBuilder[IO].withHttpApp(routes(db)) } yield server server.use(_ => IO.sleep(10.seconds)) 1. Create pool 2. Start server 3. Sleep 10 seconds
  37. RESOURCE val server = for { db <- Resource.make(IO(createPool()))(pool =>

    IO(pool.close())) server <- BlazeServerBuilder[IO].withHttpApp(routes(db)) } yield server server.use(_ => IO.sleep(10.seconds)) 1. Create pool 2. Start server 3. Sleep 10 seconds 4. Stop server
  38. RESOURCE val server = for { db <- Resource.make(IO(createPool()))(pool =>

    IO(pool.close())) server <- BlazeServerBuilder[IO].withHttpApp(routes(db)) } yield server server.use(_ => IO.sleep(10.seconds)) 1. Create pool 2. Start server 3. Sleep 10 seconds 4. Stop server 5. Close pool
  39. FS2.STREAM[F[_], A] - 0 to ∞ values - Can have

    effects in F - Supports resource acquisition/cleanup
  40. FS2.STREAM[F[_], A] - 0 to ∞ values - Can have

    effects in F - Supports resource acquisition/cleanup Stream(1,2,3)
  41. FS2.STREAM[F[_], A] - 0 to ∞ values - Can have

    effects in F - Supports resource acquisition/cleanup Stream(1,2,3) Stream.eval(IO(util.Random.nextInt))
  42. FS2.STREAM[F[_], A] - 0 to ∞ values - Can have

    effects in F - Supports resource acquisition/cleanup Stream(1,2,3) Stream.eval(IO(util.Random.nextInt)) Stream.awakeDelay[IO](1.second)
  43. FS2.STREAM[F[_], A] - 0 to ∞ values - Can have

    effects in F - Supports resource acquisition/cleanup Stream(1,2,3) Stream.eval(IO(util.Random.nextInt)) Stream.awakeDelay[IO](1.second).compile.toList: IO[List[FiniteDuration]]
  44. RESOURCE SAFETY IN HTTP4S BlazeServerBuilder[IO].resource: Resource[IO, Server[IO]] BlazeClientBuilder[IO](ec).resource: Resource[IO, Client[IO]]

    (request: Request[IO]).body: fs2.Stream[IO, Byte] (response: Response[IO]).body: fs2.Stream[IO, Byte]
  45. RESOURCE SAFETY IN HTTP4S BlazeServerBuilder[IO].resource: Resource[IO, Server[IO]] BlazeClientBuilder[IO](ec).resource: Resource[IO, Client[IO]]

    (request: Request[IO]).body: fs2.Stream[IO, Byte] (response: Response[IO]).body: fs2.Stream[IO, Byte] ...and then there's Client
  46. trait Client[F[_]] { def run(req: Request[F]) : Resource[F, Response[F]] def

    stream(req: Request[F]): Stream[F, Response[F]] }
  47. trait Client[F[_]] { def run(req: Request[F]) : Resource[F, Response[F]] def

    stream(req: Request[F]): Stream[F, Response[F]] def fetch[A](req: Request[F])(f: Response[F] => F[A]) : F[A] def expect[A](req: Request[F])(implicit d: EntityDecoder[F, A]): F[A] } (these have like 10 overloads*) *some are about to be removed
  48. trait Client[F[_]] { def run(req: Request[F]) : Resource[F, Response[F]] def

    stream(req: Request[F]): Stream[F, Response[F]] def fetch[A](req: Request[F])(f: Response[F] => F[A]) : F[A] def expect[A](req: Request[F])(implicit d: EntityDecoder[F, A]): F[A] def status(req: Request[F]). : F[Status] def successful(req: Request[F]): F[Boolean] }
  49. trait Client[F[_]] { def run(req: Request[F]) : Resource[F, Response[F]] def

    stream(req: Request[F]): Stream[F, Response[F]] def fetch[A](req: Request[F])(f: Response[F] => F[A]) : F[A] def expect[A](req: Request[F])(implicit d: EntityDecoder[F, A]): F[A] def status(req: Request[F]). : F[Status] def successful(req: Request[F]): F[Boolean] def toKleisli[A](f: Response[F] => F[A]): Kleisli[F, Request[F], A] //... }
  50. trait Client[F[_]] { def run(req: Request[F]) : Resource[F, Response[F]] def

    stream(req: Request[F]): Stream[F, Response[F]] def fetch[A](req: Request[F])(f: Response[F] => F[A]) : F[A] def expect[A](req: Request[F])(implicit d: EntityDecoder[F, A]): F[A] def status(req: Request[F]). : F[Status] def successful(req: Request[F]): F[Boolean] def toKleisli[A](f: Response[F] => F[A]): Kleisli[F, Request[F], A] //... //here be dragons - don't use unless you know what you're doing def toHttpApp: HttpApp[F] }
  51. T E S T I N G R E S

    O U R C E S A F E T Y E X T E N S I B I L I T Y
  52. T E S T I N G R E S

    O U R C E S A F E T Y E X T E N S I B I L I T Y
  53. case class Data(x: Int) val f: Data => Int =

    _.x def wrapF(f1: Data => Int): Data => Int = data => f1(data.copy(x = data.x + 1)) * 2
  54. case class Data(x: Int) val f: Data => Int =

    _.x def wrapF(f1: Data => Int): Data => Int = data => f1(data.copy(x = data.x + 1)) * 2 val f2: Data => Int = wrapF(f)
  55. case class Data(x: Int) val f: Data => Int =

    _.x val f2: Data => Int = wrapF(f) def wrapF(f1: Data => Int): Data => Int = data => f1( ) * 2 data.copy(x = data.x + 1)
  56. SERVER MIDDLEWARE: RESPONSE TIMING object ResponseTiming { def apply[F[_]]( http:

    HttpApp[F], timeUnit: TimeUnit = MILLISECONDS, headerName: CaseInsensitiveString = CaseInsensitiveString("X-Response-Time"))( implicit F: Sync[F], clock: Clock[F]): HttpApp[F] = Kleisli { req => for { before <- clock.monotonic(timeUnit) resp <- http(req) after <- clock.monotonic(timeUnit) header = Header(headerName.value, s"${after - before}") } yield resp.putHeaders(header) } }
  57. SERVER MIDDLEWARE: RESPONSE TIMING object ResponseTiming { def apply[F[_]]( http:

    HttpApp[F], timeUnit: TimeUnit = MILLISECONDS, headerName: CaseInsensitiveString = CaseInsensitiveString("X-Response-Time"))( implicit F: Sync[F], clock: Clock[F]): HttpApp[F] = Kleisli { req => for { before <- clock.monotonic(timeUnit) after <- clock.monotonic(timeUnit) header = Header(headerName.value, s"${after - before}") } yield } } Kleisli { req => resp <- http(req) resp.putHeaders(header) }
  58. SERVER MIDDLEWARE: HEADER ECHO object HeaderEcho { def apply[F[_]: Functor,

    G[_]: Functor] (echoHeadersWhen: CaseInsensitiveString => Boolean) (http: Http[F, G]): Http[F, G] = Kleisli { req: Request[G] => val headersToEcho = req.headers.filter(h => echoHeadersWhen(h.name)) http(req).map(_.putHeaders(headersToEcho.toList: _*)) } }
  59. SERVER MIDDLEWARE: HEADER ECHO object HeaderEcho { def apply[F[_]: Functor,

    G[_]: Functor] (echoHeadersWhen: CaseInsensitiveString => Boolean) = Kleisli { req: Request[G] => val headersToEcho = req.headers.filter(h => echoHeadersWhen(h.name)) http(req).map(_.putHeaders(headersToEcho.toList: _*)) } } (http: Http[F, G]): Http[F, G]
  60. CLIENT MIDDLEWARE: BASE URL object BaseUrl { def apply[F[_]](base: Uri)(

    client: Client[F] )(implicit F: Bracket[F, Throwable]): Client[F] = { Client.apply[F] { req => client.run(req.withUri(base.resolve(req.uri))) } } }
  61. ATTRIBUTION natural power by Icon Island from the Noun Project

    testing by tom from the Noun Project modules by mikicon from the Noun Project
  62. BONUS SLIDE: WEBSOCKETS case GET -> Root / "ws" /

    "echo" => Queue.bounded[IO, WebSocketFrame](100).flatMap { q => WebSocketBuilder[IO].build( send = q.dequeue, receive = q.enqueue ) }
  63. BONUS SLIDE: WEBSOCKETS case GET -> Root / "ws" /

    "echo" => Queue.bounded[IO, WebSocketFrame](100).flatMap { q => WebSocketBuilder[IO].build( ) } send = q.dequeue, receive = q.enqueue