Skip to content

Instantly share code, notes, and snippets.

@Pyppe
Created October 18, 2018 14:02
Show Gist options
  • Save Pyppe/aa032a965588eceb0fae9f6989460085 to your computer and use it in GitHub Desktop.
Save Pyppe/aa032a965588eceb0fae9f6989460085 to your computer and use it in GitHub Desktop.
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()
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment