/core/cloudflow-operator/src/main/scala/cloudflow/operator/HealthChecks.scala

https://github.com/lightbend/cloudflow · Scala · 61 lines · 41 code · 3 blank · 17 comment · 0 complexity · 19b31b9127256a6020db2c32f57528fa MD5 · raw file

  1. /*
  2. * Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cloudflow.operator
  17. import scala.concurrent._
  18. import scala.util._
  19. import akka.actor._
  20. import akka.stream._
  21. import akka.http.scaladsl._
  22. import akka.http.scaladsl.model._
  23. import akka.http.scaladsl.server.Directives._
  24. object HealthChecks {
  25. def serve(settings: Settings)(implicit system: ActorSystem, ec: ExecutionContext) = {
  26. implicit val materializer = ActorMaterializer()
  27. Http()
  28. .bindAndHandle(
  29. route,
  30. settings.api.bindInterface,
  31. settings.api.bindPort
  32. )
  33. .onComplete {
  34. case Success(serverBinding)
  35. system.log.info(s"Bound to ${serverBinding.localAddress}.")
  36. case Failure(e)
  37. system.log.error(e, s"Failed to bind.")
  38. system.terminate().foreach { _
  39. println("Exiting, could not bind http.")
  40. sys.exit(-1)
  41. }
  42. }
  43. }
  44. def route =
  45. // format: OFF
  46. path("robots.txt") {
  47. getFromResource("robots.txt")
  48. } ~
  49. pathPrefix("checks") {
  50. path("healthy") {
  51. complete(StatusCodes.OK)
  52. } ~
  53. path("ready") {
  54. complete(StatusCodes.OK)
  55. }
  56. }
  57. // format: ON
  58. }