Skip to content

Instantly share code, notes, and snippets.

@Pyppe
Created October 18, 2018 14:02

Revisions

  1. Pyppe created this gist Oct 18, 2018.
    40 changes: 40 additions & 0 deletions Http4sBlazeServerBuilder.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    object Http4sBlazeServerBuilder {

    case class Server(fiber: Fiber[IO, Nothing]) {
    def shutdownNow(): Unit = fiber.cancel.unsafeRunSync()
    }
    type MappedService = (String, HttpRoutes[IO])

    private implicit val cs = IO.contextShift(ExecutionContext.Implicits.global)

    def startServer(port: Int,
    monitoringOrdinals: Option[DevOpsController.FlowOrdinalF] = None)
    (services: MappedService*): Server = {
    DevOpsMetrics.registerDefaultMetrics()

    val withPrometheusMetrics = PrometheusMetrics[IO](CollectorRegistry.defaultRegistry, prefix = "tmnow_http_server")

    val mappings: List[MappedService] = {
    ("/devops/internal", DevOpsController.createService(monitoringOrdinals)) ::
    services.toList
    }

    Server(
    BlazeServerBuilder[IO].
    bindHttp(port, "0.0.0.0").
    withIdleTimeout(5.minutes).
    withHttpApp(
    IORequestLogger(
    withPrometheusMetrics(
    Router[IO](mappings: _*)
    ).unsafeRunSync.orNotFound
    )
    ).
    resource.
    use(_ => IO.never).
    start.
    unsafeRunSync()
    )
    }

    }