Skip to content

Commit

Permalink
http4s ember server
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Oct 16, 2020
1 parent 42ea31c commit 87950bc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ lazy val library =
val monixReactive = "io.monix" %% "monix-reactive" % Version.monix
val sbtTest = "org.scala-sbt" % "test-interface" % Version.sbtTest
val http4sClient = "org.http4s" %% "http4s-blaze-client" % Version.http4s
val http4sServer = "org.http4s" %% "http4s-blaze-server" % Version.http4s
val http4sServer = "org.http4s" %% "http4s-ember-server" % Version.http4s
val http4sCirce = "org.http4s" %% "http4s-circe" % Version.http4s
val http4sDsl = "org.http4s" %% "http4s-dsl" % Version.http4s
val fs2Io = "co.fs2" %% "fs2-io" % Version.fs2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import java.net.NetworkInterface

import com.github.agourlay.cornichon.core.CornichonError
import monix.eval.Task
import monix.execution.Scheduler
import org.http4s.HttpRoutes
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.implicits._

import scala.jdk.CollectionConverters._
Expand All @@ -19,7 +18,7 @@ class MockHttpServer[A](
interface: Option[String],
port: Option[Range],
mockService: HttpRoutes[Task],
maxPortBindingRetries: Int)(useFromAddress: String => Task[A])(implicit scheduler: Scheduler) {
maxPortBindingRetries: Int)(useFromAddress: String => Task[A]) {

private val selectedInterface = interface.getOrElse(bestInterface())
private val randomPortOrder = port.fold(0 :: Nil)(r => Random.shuffle(r.toList))
Expand All @@ -45,12 +44,11 @@ class MockHttpServer[A](
}

private def startBlazeServer(port: Int): Task[A] =
BlazeServerBuilder[Task](executionContext = scheduler)
.bindHttp(port, selectedInterface)
.withoutBanner
EmberServerBuilder.default[Task]
.withPort(port)
.withHost(selectedInterface)
.withHttpApp(mockRouter)
.withNio2(true)
.resource
.build
.use(server => useFromAddress(s"http://${server.address.getHostString}:${server.address.getPort}"))

private def bestInterface(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.http4s._
import org.http4s.implicits._
import org.http4s.dsl._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder

class ReverseAPI extends Http4sDsl[Task] {

Expand All @@ -25,11 +25,11 @@ class ReverseAPI extends Http4sDsl[Task] {
)

def start(httpPort: Int): CancelableFuture[HttpServer] =
BlazeServerBuilder[Task](executionContext = s)
.bindHttp(httpPort, "localhost")
.withoutBanner
.withNio2(true)
EmberServerBuilder.default[Task]
.withPort(httpPort)
.withHost("localhost")
.withHttpApp(routes.orNotFound)
.build
.allocated
.map { case (_, stop) => new HttpServer(stop) }
.runToFuture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.http4s._
import org.http4s.implicits._
import org.http4s.dsl._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder

class TurnstileAPI extends Http4sDsl[Task] {

Expand Down Expand Up @@ -38,11 +38,11 @@ class TurnstileAPI extends Http4sDsl[Task] {
)

def start(httpPort: Int): CancelableFuture[HttpServer] =
BlazeServerBuilder[Task](executionContext = s)
.bindHttp(httpPort, "localhost")
.withoutBanner
.withNio2(true)
EmberServerBuilder.default[Task]
.withPort(httpPort)
.withHost("localhost")
.withHttpApp(routes.orNotFound)
.build
.allocated
.map { case (_, stop) => new HttpServer(stop) }
.runToFuture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.http4s.circe._
import org.http4s.implicits._
import org.http4s.dsl._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder

import scala.collection.concurrent.TrieMap
import scala.concurrent.duration.FiniteDuration
Expand Down Expand Up @@ -127,11 +127,11 @@ class WebShopAPI(maxSyncDelay: FiniteDuration) extends Http4sDsl[Task] {
)

def start(httpPort: Int): CancelableFuture[HttpServer] =
BlazeServerBuilder[Task](executionContext = s)
.bindHttp(httpPort, "localhost")
.withoutBanner
.withNio2(true)
EmberServerBuilder.default[Task]
.withPort(httpPort)
.withHost("localhost")
.withHttpApp(routes.orNotFound)
.build
.allocated
.map { case (_, stop) => new HttpServer(stop) }
.runToFuture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import monix.eval.Task
import monix.eval.Task._
import monix.execution.{ CancelableFuture, Scheduler }
import org.http4s.server.{ AuthMiddleware, Router }
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.middleware.authentication.BasicAuth
import org.http4s.server.middleware.authentication.BasicAuth.BasicAuthenticator
import org.http4s._
Expand Down Expand Up @@ -169,11 +169,11 @@ class SuperHeroesHttpAPI() extends Http4sDsl[Task] {
)

def start(httpPort: Int): CancelableFuture[HttpServer] =
BlazeServerBuilder[Task](executionContext = s)
.bindHttp(httpPort, "localhost")
.withoutBanner
.withNio2(true)
EmberServerBuilder.default[Task]
.withPort(httpPort)
.withHost("localhost")
.withHttpApp(GZip(routes.orNotFound))
.build
.allocated
.map { case (_, stop) => new HttpServer(stop) }
.runToFuture
Expand Down

0 comments on commit 87950bc

Please sign in to comment.