diff --git a/build.sbt b/build.sbt index 03a746eee..20198ca56 100644 --- a/build.sbt +++ b/build.sbt @@ -126,7 +126,11 @@ lazy val http4s = project .settings( libraryDependencies ++= Seq( Dependencies.Libraries.http4sDsl, - Dependencies.Libraries.http4sServer, + Dependencies.Libraries.http4sEmber, + Dependencies.Libraries.http4sBlaze, + Dependencies.Libraries.http4sNetty, + Dependencies.Libraries.log4cats, + Dependencies.Libraries.slf4j, Dependencies.Libraries.specs2 ) ) diff --git a/http4s/src/main/scala/com.snowplowanalytics.snowplow.collectors.scalastream/CollectorApp.scala b/http4s/src/main/scala/com.snowplowanalytics.snowplow.collectors.scalastream/CollectorApp.scala index 14628d0b5..fbdbce6e4 100644 --- a/http4s/src/main/scala/com.snowplowanalytics.snowplow.collectors.scalastream/CollectorApp.scala +++ b/http4s/src/main/scala/com.snowplowanalytics.snowplow.collectors.scalastream/CollectorApp.scala @@ -1,19 +1,58 @@ package com.snowplowanalytics.snowplow.collectors.scalastream +import cats.implicits._ import cats.effect.{ExitCode, IO} +import cats.effect.kernel.Resource import com.comcast.ip4s.IpLiteralSyntax +import org.http4s.server.Server import org.http4s.ember.server.EmberServerBuilder +import org.http4s.blaze.server.BlazeServerBuilder +import org.http4s.netty.server.NettyServerBuilder +import org.typelevel.log4cats.Logger +import org.typelevel.log4cats.slf4j.Slf4jLogger + +import java.net.InetSocketAddress +import scala.concurrent.duration.DurationLong object CollectorApp { + implicit private def unsafeLogger: Logger[IO] = + Slf4jLogger.getLogger[IO] + def run(): IO[ExitCode] = buildHttpServer().use(_ => IO.never).as(ExitCode.Success) - private def buildHttpServer() = - EmberServerBuilder - .default[IO] - .withHost(ipv4"0.0.0.0") - .withPort(port"8080") - .withHttpApp(new CollectorRoutes[IO].value) - .build + private def buildHttpServer(): Resource[IO, Server] = + sys.env.get("HTTP4S_BACKEND").map(_.toUpperCase()) match { + case Some("EMBER") | None => buildEmberServer + case Some("BLAZE") => buildBlazeServer + case Some("NETTY") => buildNettyServer + case Some(other) => throw new IllegalArgumentException(s"Unrecognized http4s backend $other") + } + + private def buildEmberServer = + Resource.eval(Logger[IO].info("Building ember server")) >> + EmberServerBuilder + .default[IO] + .withHost(ipv4"0.0.0.0") + .withPort(port"8080") + .withHttpApp(new CollectorRoutes[IO].value) + .withIdleTimeout(610.seconds) + .build + + private def buildBlazeServer: Resource[IO, Server] = + Resource.eval(Logger[IO].info("Building blaze server")) >> + BlazeServerBuilder[IO] + .bindSocketAddress(new InetSocketAddress(8080)) + .withHttpApp(new CollectorRoutes[IO].value) + .withIdleTimeout(610.seconds) + .resource + + private def buildNettyServer: Resource[IO, Server] = + Resource.eval(Logger[IO].info("Building netty server")) >> + NettyServerBuilder[IO] + .bindLocal(8080) + .withHttpApp(new CollectorRoutes[IO].value) + .withIdleTimeout(610.seconds) + .resource } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index a384a02e8..6f225e087 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -47,12 +47,15 @@ object Dependencies { val pureconfig = "0.17.2" val akkaHttpMetrics = "1.7.1" val badRows = "2.1.1" + val log4cats = "2.6.0" // Scala (test only) val specs2 = "4.11.0" val specs2CE = "0.4.1" val testcontainers = "0.40.10" val catsRetry = "2.1.0" val http4s = "0.23.23" + val blaze = "0.23.15" + val http4sNetty = "0.5.9" val http4sIT = "0.21.33" } @@ -87,11 +90,14 @@ object Dependencies { val akkaSlf4j = "com.typesafe.akka" %% "akka-slf4j" % V.akka val pureconfig = "com.github.pureconfig" %% "pureconfig" % V.pureconfig val akkaHttpMetrics = "fr.davit" %% "akka-http-metrics-datadog" % V.akkaHttpMetrics + val log4cats = "org.typelevel" %% "log4cats-slf4j" % V.log4cats //http4s val http4sDsl = "org.http4s" %% "http4s-dsl" % V.http4s - val http4sServer = "org.http4s" %% "http4s-ember-server" % V.http4s + val http4sEmber = "org.http4s" %% "http4s-ember-server" % V.http4s + val http4sBlaze = "org.http4s" %% "http4s-blaze-server" % V.blaze + val http4sNetty = "org.http4s" %% "http4s-netty-server" % V.http4sNetty // Scala (test only) val specs2 = "org.specs2" %% "specs2-core" % V.specs2 % Test