|
| 1 | +# Module http4s |
| 2 | + |
| 3 | +[](https://repo1.maven.org/maven2/com/avast/scala-server-toolkit-http4s-blaze-server_2.12/) |
| 4 | + |
| 5 | +`libraryDependencies += "com.avast" %% "scala-server-toolkit-http4s-blaze-server" % "<VERSION>"` |
| 6 | + |
| 7 | +There are `http4s-*` modules that provide easy initialization of a server and a client. Http4s is an interface with multiple possible |
| 8 | +implementations - for now we provide only implementations based on [Blaze](https://github.com/http4s/blaze). |
| 9 | + |
| 10 | +Both server and client are configured via configuration `case class` which contains default values taken from the underlying implementations. |
| 11 | + |
| 12 | +```scala mdoc:silent:reset-class |
| 13 | +import cats.effect._ |
| 14 | +import com.avast.server.toolkit.execution.ExecutorModule |
| 15 | +import com.avast.server.toolkit.http4s._ |
| 16 | +import com.avast.server.toolkit.system.console.ConsoleModule |
| 17 | +import org.http4s.dsl.Http4sDsl |
| 18 | +import org.http4s.HttpRoutes |
| 19 | +import zio.DefaultRuntime |
| 20 | +import zio.interop.catz._ |
| 21 | +import zio.interop.catz.implicits._ |
| 22 | +import zio.Task |
| 23 | + |
| 24 | +implicit val runtime = new DefaultRuntime {} // this is just needed in example |
| 25 | + |
| 26 | +val dsl = Http4sDsl[Task] // this is just needed in example |
| 27 | +import dsl._ |
| 28 | + |
| 29 | +val routes = Http4sRouting.make { |
| 30 | + HttpRoutes.of[Task] { |
| 31 | + case GET -> Root / "hello" => Ok("Hello World!") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +val resource = for { |
| 36 | + executorModule <- ExecutorModule.makeDefault[Task] |
| 37 | + console = ConsoleModule.make[Task] |
| 38 | + server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig("127.0.0.1", 0), routes, executorModule.executionContext) |
| 39 | + client <- Http4sBlazeClient.make[Task](Http4sBlazeClientConfig(), executorModule.executionContext) |
| 40 | +} yield (server, client, console) |
| 41 | + |
| 42 | +val program = resource |
| 43 | + .use { |
| 44 | + case (server, client, console) => |
| 45 | + client |
| 46 | + .expect[String](s"http://127.0.0.1:${server.address.getPort}/hello") |
| 47 | + .flatMap(console.printLine) |
| 48 | + } |
| 49 | +``` |
| 50 | + |
| 51 | +```scala mdoc |
| 52 | +runtime.unsafeRun(program) |
| 53 | +``` |
| 54 | + |
| 55 | +## Middleware |
| 56 | + |
| 57 | +### Correlation ID Middleware |
| 58 | + |
| 59 | +```scala mdoc:silent:reset |
| 60 | +import cats.effect._ |
| 61 | +import com.avast.server.toolkit.execution.ExecutorModule |
| 62 | +import com.avast.server.toolkit.http4s._ |
| 63 | +import com.avast.server.toolkit.http4s.middleware.CorrelationIdMiddleware |
| 64 | +import org.http4s.dsl.Http4sDsl |
| 65 | +import org.http4s.HttpRoutes |
| 66 | +import zio.DefaultRuntime |
| 67 | +import zio.interop.catz._ |
| 68 | +import zio.interop.catz.implicits._ |
| 69 | +import zio.Task |
| 70 | + |
| 71 | +val dsl = Http4sDsl[Task] // this is just needed in example |
| 72 | +import dsl._ |
| 73 | + |
| 74 | +implicit val runtime = new DefaultRuntime {} // this is just needed in example |
| 75 | + |
| 76 | +for { |
| 77 | + middleware <- Resource.liftF(CorrelationIdMiddleware.default[Task]) |
| 78 | + executorModule <- ExecutorModule.makeDefault[Task] |
| 79 | + routes = Http4sRouting.make { |
| 80 | + middleware.wrap { |
| 81 | + HttpRoutes.of[Task] { |
| 82 | + case req @ GET -> Root => |
| 83 | + // val correlationId = middleware.retrieveCorrelationId(req) |
| 84 | + ??? |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + server <- Http4sBlazeServerModule.make[Task](Http4sBlazeServerConfig.localhost8080, routes, executorModule.executionContext) |
| 89 | +} yield server |
| 90 | +``` |
0 commit comments