diff --git a/build.sbt b/build.sbt index d3843bb34..dc020e98c 100644 --- a/build.sbt +++ b/build.sbt @@ -21,6 +21,7 @@ lazy val core = project Dependencies.Libraries.http4sDsl, Dependencies.Libraries.http4sBlaze, Dependencies.Libraries.http4sEmber, + Dependencies.Libraries.http4sNetty, Dependencies.Libraries.http4sClient, Dependencies.Libraries.log4cats, Dependencies.Libraries.thrift, diff --git a/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/Config.scala b/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/Config.scala index a3d676be8..ce6d01652 100644 --- a/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/Config.scala +++ b/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/Config.scala @@ -182,6 +182,7 @@ object Config { object Backend { case object Blaze extends Backend case object Ember extends Backend + case object Netty extends Backend } } @@ -224,6 +225,7 @@ object Config { implicit val backend: Decoder[Experimental.Backend] = Decoder[String].emap { case s if s.toLowerCase() == "blaze" => Right(Experimental.Backend.Blaze) case s if s.toLowerCase() == "ember" => Right(Experimental.Backend.Ember) + case s if s.toLowerCase() == "netty" => Right(Experimental.Backend.Netty) case other => Left(s"Invalid backend $other") } implicit val experimental = deriveDecoder[Experimental] diff --git a/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/HttpServer.scala b/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/HttpServer.scala index 560a7d23d..62d6891b8 100644 --- a/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/HttpServer.scala +++ b/core/src/main/scala/com.snowplowanalytics.snowplow.collector.core/HttpServer.scala @@ -17,7 +17,8 @@ import com.avast.datadog4s.extension.http4s.DatadogMetricsOps import com.avast.datadog4s.{StatsDMetricFactory, StatsDMetricFactoryConfig} import org.http4s.{HttpApp, HttpRoutes} import org.http4s.blaze.server.BlazeServerBuilder -import org.http4s.ember.server._ +import org.http4s.ember.server.EmberServerBuilder +import org.http4s.netty.server.NettyServerBuilder import com.comcast.ip4s._ import fs2.io.net.Network import fs2.io.net.tls.TLSContext @@ -144,6 +145,27 @@ object HttpServer { .build } + private def buildNettyServer[F[_]: Async: Network]( + routes: HttpRoutes[F], + port: Int, + secure: Boolean, + hsts: Config.HSTS, + networking: Config.Networking, + debugHttp: Config.Debug.Http + ): Resource[F, Server] = + Resource.eval(TLSContext.Builder.forAsync[F].system).flatMap { tls => + Resource.eval(Logger[F].info("Building netty server")) >> + NettyServerBuilder[F] + .bindHttp(port) + .withHttpApp( + loggerMiddleware(timeoutMiddleware(hstsMiddleware(hsts, routes.orNotFound), networking), debugHttp) + ) + .withIdleTimeout(networking.idleTimeout) + .withMaxInitialLineLength(networking.maxRequestLineLength) + .cond(secure, _.withSslContext(SSLContext.getDefault)) + .build + } + implicit class ConditionalAction[A](item: A) { def cond(cond: Boolean, action: A => A): A = if (cond) action(item) else item diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 7d3c3ffb7..22b76d411 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -24,6 +24,7 @@ object Dependencies { val decline = "2.4.1" val fs2PubSub = "0.22.0" val http4s = "0.23.23" + val http4sNetty = "0.5.16" val jackson = "2.12.7" // force this version to mitigate security vulnerabilities val fs2Kafka = "2.6.1" val log4cats = "2.6.0" @@ -54,6 +55,7 @@ object Dependencies { val collectorPayload = "com.snowplowanalytics" % "collector-payload-1" % V.collectorPayload val decline = "com.monovore" %% "decline-effect" % V.decline val emitterHttps = "com.snowplowanalytics" %% "snowplow-scala-tracker-emitter-http4s" % V.tracker + val http4sNetty = "org.http4s" %% "http4s-netty-server" % V.http4sNetty val http4sEmber = "org.http4s" %% "http4s-ember-server" % V.http4s val http4sBlaze = "org.http4s" %% "http4s-blaze-server" % V.blaze val http4sClient = "org.http4s" %% "http4s-blaze-client" % V.blaze