Permalink
Browse files

=> cats.effect.IO (#45)

No more `scala.concurrent.Future` in the library API. Instead we use `cats.effect.IO`
everywhere. It is more coherent especially when we have to deal with effects consuming
the content stream: compose IO all the way instead of dealing with IO/Future transformation
everywhere.

Note that `IO` being lazy some operations are now lazy. For example `Client.run` or `client.stop`
do nothing if just called imperatively. Synchronous versions of these operations have been added
to make it easier to use the client in an imperative way if needed (`Client.runSync` or `client.stopSync`).
  • Loading branch information...
guillaumebort committed Nov 10, 2017
1 parent 254108d commit 70eee6f8b01d8c902b2f35db7867246cc7458e75
Showing with 435 additions and 513 deletions.
  1. +13 −8 README.md
  2. +6 −7 build.sbt
  3. +0 −26 core/src/main/scala-2.11/internal/FutureLikeResponse.scala
  4. +0 −14 core/src/main/scala-2.11/internal/KillableFuture.scala
  5. +0 −30 core/src/main/scala-2.12/internal/FutureLikeResponse.scala
  6. +0 −16 core/src/main/scala-2.12/internal/KillableFuture.scala
  7. +191 −120 core/src/main/scala/Client.scala
  8. +11 −17 core/src/main/scala/Content.scala
  9. +4 −7 core/src/main/scala/Request.scala
  10. +22 −29 core/src/main/scala/Response.scala
  11. +9 −8 core/src/main/scala/Server.scala
  12. +16 −22 core/src/main/scala/internal/NettySupport.scala
  13. +13 −11 core/src/main/scala/internal/internal.scala
  14. +15 −6 core/src/main/scala/package.scala
  15. +30 −34 core/src/test/scala/ClientTests.scala
  16. +6 −11 core/src/test/scala/ConnectionUpgradeTests.scala
  17. +4 −5 core/src/test/scala/ContentTests.scala
  18. +0 −26 core/src/test/scala/ResponseTests.scala
  19. +4 −5 core/src/test/scala/ServerSentEventsTests.scala
  20. +1 −1 core/src/test/scala/ServerTests.scala
  21. +12 −19 core/src/test/scala/Tests.scala
  22. +20 −22 examples/src/it/scala/ExamplesTests.scala
  23. +1 −2 examples/src/it/scala/ProtocolNegociationTests.scala
  24. +1 −3 examples/src/it/scala/StreamingTests.scala
  25. +13 −18 examples/src/it/scala/StressTests.scala
  26. +23 −25 examples/src/main/scala/GithubClient.scala
  27. +1 −2 examples/src/main/scala/HelloWorld.scala
  28. +1 −2 examples/src/main/scala/Http2Server.scala
  29. +10 −6 examples/src/main/scala/JsonWebService.scala
  30. +1 −2 examples/src/main/scala/LargeFileUpload.scala
  31. +1 −2 examples/src/main/scala/ReverseProxy.scala
  32. +1 −2 examples/src/main/scala/ServingFiles.scala
  33. +5 −5 json/src/test/scala/JsonTests.scala
View
@@ -2,11 +2,11 @@
# lolhttp
An HTTP & HTTP/2 server and client library for Scala.
An HTTP & HTTP/2 Server and Client library for Scala.
## About the library
Both server and client are plain functions accepting an HTTP request and eventually giving back an HTTP response. Requests and responses are just HTTP metadata along with a lazy content body. The content body is a stream of bytes based on [fs2](https://github.com/functional-streams-for-scala/fs2), making it easy to handle streaming scenarios if needed. For additional convenience, the library provides content encoders and decoders for the common scala types. SSL is supported on both sides.
A server is built from a plain function accepting an HTTP request and eventually giving back an HTTP response. The computation is wrapped into an [effect](https://github.com/typelevel/cats-effect) and can handle pure & impure and synchronous & asynchronous scenarios. Requests and responses are just HTTP metadata along with a lazy content body based on [fs2](https://github.com/functional-streams-for-scala/fs2), making it easy to handle streaming scenarios if needed. For additional convenience, the library provides content encoders and decoders for the common scala types. Clients and Servers share the same API and the same concepts, making it easy to compose them. SSL is supported on both sides.
## Hello World
@@ -20,7 +20,7 @@ Server.listen(8888) {
}
// Let's connect with an HTTP client
Client.run(Get("http://localhost:8888/hello")) { res =>
Client.runSync(Get("http://localhost:8888/hello")) { res =>
res.readAs[String].map { contentBody =>
println(s"Received: $contentBody")
}
@@ -29,22 +29,27 @@ Client.run(Get("http://localhost:8888/hello")) { res =>
## About HTTP/2 support
HTTP/2 is supported on both server and client side. If SSL is enabled, the protocol negociation is done using ALPN. On plain connections however HTTP/2 is only supported with prior knowledge (clear text upgrade from HTTP/1.1 to HTTP/2 is ignored). Because of ALPN, HTTP/2 support over SSL requires running on Java 9 (_Running on Java 8 is still possible but you need to replace the default Java TLS implementation; see http://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn-starting_).
HTTP/2 is supported on both Server and Client side. If SSL is enabled, the protocol negociation is done using ALPN. On plain connections however HTTP/2 is only supported with prior knowledge (clear text upgrade from HTTP/1.1 to HTTP/2 is ignored). Because of ALPN, HTTP/2 support over SSL requires running on Java 9 (_Running on Java 8 is still possible but you need to replace the default Java TLS implementation; see http://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn-starting_).
## Usage
The library is cross-built for __Scala 2.11__ and __Scala 2.12__.
The core module to use is `"com.criteo.lolhttp" %% "lolhttp" % "0.7.4"`.
The core module to use is `"com.criteo.lolhttp" %% "lolhttp" % "0.8.0"`.
There are also 2 optional companion libraries:
- `"com.criteo.lolhttp" %% "loljson" % "0.7.4"`, provides integration with the [circe](https://circe.github.io/circe/) JSON library.
- `"com.criteo.lolhttp" %% "lolhtml" % "0.7.4"`, provides minimal HTML templating.
- `"com.criteo.lolhttp" %% "loljson" % "0.8.0"`, provides integration with the [circe](https://circe.github.io/circe/) JSON library.
- `"com.criteo.lolhttp" %% "lolhtml" % "0.8.0"`, provides minimal HTML templating.
## Documentation
The [API documentation](https://criteo.github.io/lolhttp/api/lol/index.html) is the main reference. If you need to access the underlying content stream, you should first have a look a the [fs2 documentation](https://github.com/functional-streams-for-scala/fs2) to understand the basics.
The [API documentation](https://criteo.github.io/lolhttp/api/lol/index.html) is the main reference.
If you never used [cats effects](https://github.com/typelevel/cats-effect) before, you should start by having a quick look at it. Service [Responses](https://criteo.github.io/lolhttp/api/lol/http/Response.html) must be wrapped
into an `IO` (although there is an implicit conversion translating `Response` into a pure `IO` if needed). Also consuming the underlying content stream data is an effect. For asynchronous scenarios you have to deal with asynchronous effects, unless you prefer working with `Future` and wrapping it at the end using `IO.fromFuture`.
If you need to access the underlying content stream, you should first have a look a the [fs2 documentation](https://github.com/functional-streams-for-scala/fs2) to understand the basics.
For those who prefer documentation by example, you can also follow these hands-on introductions:
View
@@ -1,10 +1,10 @@
val VERSION = "0.7.4"
val VERSION = "0.8.0"
lazy val commonSettings = Seq(
organization := "com.criteo.lolhttp",
version := VERSION,
scalaVersion := "2.12.2",
crossScalaVersions := Seq("2.11.11", "2.12.3"),
scalaVersion := "2.12.4",
crossScalaVersions := Seq("2.11.11", scalaVersion.value),
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
@@ -13,7 +13,6 @@ lazy val commonSettings = Seq(
"-Xlint",
"-Yno-adapted-args",
"-Ywarn-dead-code",
"-language:postfixOps",
"-Xfuture",
"-Ywarn-unused-import"
),
@@ -94,9 +93,9 @@ lazy val lolhttp =
commonSettings,
libraryDependencies ++= Seq(
"co.fs2" %% "fs2-core" % "0.10.0-M6",
"co.fs2" %% "fs2-core" % "0.10.0-M8",
"io.netty" % "netty-codec-http2" % "4.1.16.Final",
"org.scalatest" %% "scalatest" % "3.0.1" % "test"
"org.scalatest" %% "scalatest" % "3.0.4" % "test"
),
// Vendorise internal libs
@@ -137,7 +136,7 @@ lazy val loljson =
"io.circe" %% "circe-generic",
"io.circe" %% "circe-parser",
"io.circe" %% "circe-optics"
).map(_ % "0.9.0-M1"),
).map(_ % "0.9.0-M2"),
pomPostProcess := removeDependencies("org.scalatest")
).
dependsOn(lolhttp % "compile->compile;test->test")

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 70eee6f

Please sign in to comment.