Skip to content
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

Environment variable HTTP4S_BACKEND to help development #320

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 7 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
Expand Down
Loading