-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add http4s client and server modules with simple tests #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
07e3f7c
feat: Add http4s client and server modules with simple tests
jakubjanecek f96931a
refactor: PR review changes
jakubjanecek e0237c3
refactor: Put CorrelationIdMiddleware to subpackage and add test
jakubjanecek b79f01f
refactor: Remove unneeded file (added by mistake)
jakubjanecek ade1f62
docs: Remove useless sentence
jakubjanecek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Module http4s | ||
|
|
||
| [](https://repo1.maven.org/maven2/com/avast/scala-server-toolkit-http4s-blaze-server_2.12/) | ||
|
|
||
| `libraryDependencies += "com.avast" %% "scala-server-toolkit-http4s-blaze-server" % "<VERSION>"` | ||
|
|
||
| There are `http4s-*` modules that provide easy initialization of a server and a client. Http4s is an interface with multiple possible | ||
| implementations - for now we provide only implementations based on [Blaze](https://github.com/http4s/blaze). | ||
|
|
||
| Both server and client are configured via configuration `case class` which contains default values taken from the underlying implementations. | ||
|
|
||
| ```scala | ||
| import cats.effect._ | ||
| import com.avast.server.toolkit.execution.ExecutorModule | ||
| import com.avast.server.toolkit.http4s._ | ||
| import com.avast.server.toolkit.system.console.ConsoleModule | ||
| import org.http4s.dsl.Http4sDsl | ||
| import org.http4s.HttpRoutes | ||
| import zio.DefaultRuntime | ||
| import zio.interop.catz._ | ||
| import zio.interop.catz.implicits._ | ||
| import zio.Task | ||
|
|
||
| implicit val runtime = new DefaultRuntime {} // this is just needed in example | ||
|
|
||
| val dsl = Http4sDsl[Task] // this is just needed in example | ||
| import dsl._ | ||
|
|
||
| val routes = Http4sRouting.make { | ||
| HttpRoutes.of[Task] { | ||
| case GET -> Root / "hello" => Ok("Hello World!") | ||
| } | ||
| } | ||
|
|
||
| val resource = for { | ||
| executorModule <- ExecutorModule.makeDefault[Task] | ||
| console = ConsoleModule.make[Task] | ||
| server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig("127.0.0.1", 0), routes, executorModule.executionContext) | ||
| client <- Http4sBlazeClient.make[Task](Http4sBlazeClientConfig(), executorModule.executionContext) | ||
| } yield (server, client, console) | ||
|
|
||
| val program = resource | ||
| .use { | ||
| case (server, client, console) => | ||
| client | ||
| .expect[String](s"http://127.0.0.1:${server.address.getPort}/hello") | ||
| .flatMap(console.printLine) | ||
| } | ||
| ``` | ||
|
|
||
| ```scala | ||
| runtime.unsafeRun(program) | ||
| // Hello World! | ||
| ``` | ||
|
|
||
| ## Middleware | ||
|
|
||
| ### Correlation ID Middleware | ||
|
|
||
| ```scala | ||
| import cats.effect._ | ||
| import com.avast.server.toolkit.execution.ExecutorModule | ||
| import com.avast.server.toolkit.http4s._ | ||
| import com.avast.server.toolkit.http4s.middleware.CorrelationIdMiddleware | ||
| import org.http4s.dsl.Http4sDsl | ||
| import org.http4s.HttpRoutes | ||
| import zio.DefaultRuntime | ||
| import zio.interop.catz._ | ||
| import zio.interop.catz.implicits._ | ||
| import zio.Task | ||
|
|
||
| val dsl = Http4sDsl[Task] // this is just needed in example | ||
| import dsl._ | ||
|
|
||
| implicit val runtime = new DefaultRuntime {} // this is just needed in example | ||
|
|
||
| for { | ||
| middleware <- Resource.liftF(CorrelationIdMiddleware.default[Task]) | ||
| executorModule <- ExecutorModule.makeDefault[Task] | ||
| routes = Http4sRouting.make { | ||
| middleware.wrap { | ||
| HttpRoutes.of[Task] { | ||
| case req @ GET -> Root => | ||
| // val correlationId = middleware.retrieveCorrelationId(req) | ||
| ??? | ||
| } | ||
| } | ||
| } | ||
| server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig.localhost8080, routes, executorModule.executionContext) | ||
| } yield server | ||
| ``` | ||
|
|
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Module http4s | ||
|
|
||
| [](https://repo1.maven.org/maven2/com/avast/scala-server-toolkit-http4s-blaze-server_2.12/) | ||
|
|
||
| `libraryDependencies += "com.avast" %% "scala-server-toolkit-http4s-blaze-server" % "<VERSION>"` | ||
|
|
||
| There are `http4s-*` modules that provide easy initialization of a server and a client. Http4s is an interface with multiple possible | ||
| implementations - for now we provide only implementations based on [Blaze](https://github.com/http4s/blaze). | ||
|
|
||
| Both server and client are configured via configuration `case class` which contains default values taken from the underlying implementations. | ||
|
|
||
| ```scala mdoc:silent:reset-class | ||
| import cats.effect._ | ||
| import com.avast.server.toolkit.execution.ExecutorModule | ||
| import com.avast.server.toolkit.http4s._ | ||
| import com.avast.server.toolkit.system.console.ConsoleModule | ||
| import org.http4s.dsl.Http4sDsl | ||
| import org.http4s.HttpRoutes | ||
| import zio.DefaultRuntime | ||
| import zio.interop.catz._ | ||
| import zio.interop.catz.implicits._ | ||
| import zio.Task | ||
|
|
||
| implicit val runtime = new DefaultRuntime {} // this is just needed in example | ||
|
|
||
| val dsl = Http4sDsl[Task] // this is just needed in example | ||
| import dsl._ | ||
|
|
||
| val routes = Http4sRouting.make { | ||
| HttpRoutes.of[Task] { | ||
| case GET -> Root / "hello" => Ok("Hello World!") | ||
| } | ||
| } | ||
|
|
||
| val resource = for { | ||
| executorModule <- ExecutorModule.makeDefault[Task] | ||
| console = ConsoleModule.make[Task] | ||
| server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig("127.0.0.1", 0), routes, executorModule.executionContext) | ||
| client <- Http4sBlazeClient.make[Task](Http4sBlazeClientConfig(), executorModule.executionContext) | ||
| } yield (server, client, console) | ||
|
|
||
| val program = resource | ||
| .use { | ||
| case (server, client, console) => | ||
| client | ||
| .expect[String](s"http://127.0.0.1:${server.address.getPort}/hello") | ||
| .flatMap(console.printLine) | ||
| } | ||
| ``` | ||
|
|
||
| ```scala mdoc | ||
| runtime.unsafeRun(program) | ||
| ``` | ||
|
|
||
| ## Middleware | ||
|
|
||
| ### Correlation ID Middleware | ||
|
|
||
| ```scala mdoc:silent:reset | ||
| import cats.effect._ | ||
| import com.avast.server.toolkit.execution.ExecutorModule | ||
| import com.avast.server.toolkit.http4s._ | ||
| import com.avast.server.toolkit.http4s.middleware.CorrelationIdMiddleware | ||
| import org.http4s.dsl.Http4sDsl | ||
| import org.http4s.HttpRoutes | ||
| import zio.DefaultRuntime | ||
| import zio.interop.catz._ | ||
| import zio.interop.catz.implicits._ | ||
| import zio.Task | ||
|
|
||
| val dsl = Http4sDsl[Task] // this is just needed in example | ||
| import dsl._ | ||
|
|
||
| implicit val runtime = new DefaultRuntime {} // this is just needed in example | ||
|
|
||
| for { | ||
| middleware <- Resource.liftF(CorrelationIdMiddleware.default[Task]) | ||
| executorModule <- ExecutorModule.makeDefault[Task] | ||
| routes = Http4sRouting.make { | ||
| middleware.wrap { | ||
| HttpRoutes.of[Task] { | ||
| case req @ GET -> Root => | ||
| // val correlationId = middleware.retrieveCorrelationId(req) | ||
| ??? | ||
| } | ||
| } | ||
| } | ||
| server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig.localhost8080, routes, executorModule.executionContext) | ||
| } yield server | ||
| ``` |
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
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
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
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
50 changes: 50 additions & 0 deletions
50
http4s-blaze-client/src/main/scala/com/avast/server/toolkit/http4s/Http4sBlazeClient.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.avast.server.toolkit.http4s | ||
|
|
||
| import cats.Traverse | ||
| import cats.effect.{ConcurrentEffect, Resource, Sync} | ||
| import cats.implicits._ | ||
| import com.avast.server.toolkit.ssl.{SslContextConfig, SslContextModule} | ||
| import javax.net.ssl.SSLContext | ||
| import org.http4s.client.Client | ||
| import org.http4s.client.blaze.BlazeClientBuilder | ||
|
|
||
| import scala.concurrent.ExecutionContext | ||
| import scala.language.higherKinds | ||
|
|
||
| object Http4sBlazeClient { | ||
|
|
||
| /** Makes [[org.http4s.client.Client]] (Blaze) initialized with the given config. | ||
| * | ||
| * @param executionContext callback handling [[scala.concurrent.ExecutionContext]] | ||
| */ | ||
| def make[F[_]: ConcurrentEffect](config: Http4sBlazeClientConfig, executionContext: ExecutionContext): Resource[F, Client[F]] = { | ||
| for { | ||
| maybeSslContext <- Resource.liftF(sslContext(config.sslContext)) | ||
jakubjanecek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| client <- { | ||
| val builder = BlazeClientBuilder[F](executionContext) | ||
| .withResponseHeaderTimeout(config.responseHeaderTimeout) | ||
| .withIdleTimeout(config.idleTimeout) | ||
| .withRequestTimeout(config.requestTimeout) | ||
| .withConnectTimeout(config.connectTimeout) | ||
| .withUserAgent(config.userAgent) | ||
| .withMaxTotalConnections(config.maxTotalConnections) | ||
| .withMaxWaitQueueLimit(config.maxWaitQueueLimit) | ||
| .withMaxConnectionsPerRequestKey(Function.const(config.maxConnectionsPerRequestkey)) | ||
| .withCheckEndpointAuthentication(config.checkEndpointIdentification) | ||
| .withMaxResponseLineSize(config.maxResponseLineSize) | ||
| .withMaxHeaderLength(config.maxHeaderLength) | ||
| .withMaxChunkSize(config.maxChunkSize) | ||
| .withChunkBufferMaxSize(config.chunkBufferMaxSize) | ||
| .withParserMode(config.parserMode) | ||
| .withBufferSize(config.bufferSize) | ||
|
|
||
| maybeSslContext.map(builder.withSslContext).getOrElse(builder).resource | ||
| } | ||
| } yield client | ||
| } | ||
|
|
||
| private def sslContext[F[_]: Sync](maybeSslContextConfig: Option[SslContextConfig]): F[Option[SSLContext]] = { | ||
| Traverse[Option].traverse(maybeSslContextConfig)(SslContextModule.make[F]) | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.