All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.co.unclealex.diagnostics.HealthCheckController.scala Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package uk.co.unclealex.diagnostics

import java.util.{SortedMap => JavaSortedMap}

import com.codahale.metrics.health.{HealthCheck, HealthCheckRegistry}
import com.codahale.metrics.json.HealthCheckModule
import com.fasterxml.jackson.databind.ObjectMapper
import play.api.mvc._

import scala.collection.JavaConverters._

/**
  * A simple controller that performs a health-check when called..
  */
class HealthCheckController(healthCheckRegistry: HealthCheckRegistry,
                            val bearerToken: String,
                            override val controllerComponents: ControllerComponents)
  extends AbstractController(controllerComponents) with SecurableByBearerToken {

  val objectMapper = new ObjectMapper()
  objectMapper.registerModule(new HealthCheckModule())

  def healthCheck: Action[AnyContent] = SecuredByBearerToken {
    Action { implicit request =>
      val healthCheckResults: JavaSortedMap[String, HealthCheck.Result] = healthCheckRegistry.runHealthChecks()
      val healthCheckJson: String = objectMapper.writeValueAsString(healthCheckResults)
      val isHealthy: Boolean = healthCheckResults.values().asScala.forall(_.isHealthy)
      val status: Int = if (isHealthy) OK else INTERNAL_SERVER_ERROR
      Status(status)(healthCheckJson)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy