/engine/standalone/app/src/main/scala/pl/touk/nussknacker/engine/standalone/http/StandaloneHttpApp.scala
https://github.com/TouK/nussknacker · Scala · 89 lines · 64 code · 25 blank · 0 comment · 0 complexity · ffa63a8917b0db39f536d3891049f78f MD5 · raw file
- package pl.touk.nussknacker.engine.standalone.http
- import akka.actor.ActorSystem
- import akka.http.scaladsl.Http
- import akka.http.scaladsl.server.Directives
- import akka.stream.ActorMaterializer
- import com.typesafe.config.{Config, ConfigFactory}
- import com.typesafe.scalalogging.LazyLogging
- import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
- import io.dropwizard.metrics5.MetricRegistry
- import pl.touk.nussknacker.engine.standalone.deployment.DeploymentService
- import pl.touk.nussknacker.engine.standalone.utils.StandaloneContextPreparer
- import pl.touk.nussknacker.engine.standalone.utils.logging.StandaloneRequestResponseLogger
- import pl.touk.nussknacker.engine.standalone.utils.metrics.dropwizard.{DropwizardMetricsProvider, StandaloneMetricsReporter}
- import pl.touk.nussknacker.engine.util.loader.ScalaServiceLoader
- import scala.util.Try
- import scala.util.control.NonFatal
- object StandaloneHttpApp extends Directives with FailFastCirceSupport with LazyLogging with App {
- private val config = ConfigFactory.load()
- implicit val system: ActorSystem = ActorSystem("nussknacker-standalone-http", config)
- import system.dispatcher
- implicit private val materializer: ActorMaterializer = ActorMaterializer()
- val metricRegistry = StandaloneMetrics.prepareRegistry(config)
- val standaloneApp = new StandaloneHttpApp(config, metricRegistry)
- val managementPort = Try(args(0).toInt).getOrElse(8070)
- val processesPort = Try(args(1).toInt).getOrElse(8080)
- Http().bindAndHandle(
- standaloneApp.managementRoute.route,
- interface = "0.0.0.0",
- port = managementPort
- )
- Http().bindAndHandle(
- standaloneApp.processRoute.route(StandaloneRequestResponseLogger.get(Thread.currentThread.getContextClassLoader)),
- interface = "0.0.0.0",
- port = processesPort
- )
- }
- object StandaloneMetrics extends LazyLogging {
- def prepareRegistry(config: Config): MetricRegistry = {
- val metricRegistry = new MetricRegistry
- val metricReporters = loadMetricsReporters()
- metricReporters.foreach { reporter =>
- reporter.createAndRunReporter(metricRegistry, config)
- }
- metricRegistry
- }
- private def loadMetricsReporters(): List[StandaloneMetricsReporter] = {
- try {
- val reporters = ScalaServiceLoader.load[StandaloneMetricsReporter](Thread.currentThread().getContextClassLoader)
- logger.info(s"Loaded metrics reporters: ${reporters.map(_.getClass.getCanonicalName).mkString(", ")}")
- reporters
- } catch {
- case NonFatal(ex) =>
- logger.warn("Metrics reporter load failed. There will be no metrics reporter in standalone", ex)
- List.empty
- }
- }
- }
- class StandaloneHttpApp(config: Config, metricRegistry: MetricRegistry)(implicit as: ActorSystem)
- extends Directives with LazyLogging {
- private val contextPreparer = new StandaloneContextPreparer(new DropwizardMetricsProvider(metricRegistry))
- private val deploymentService = DeploymentService(contextPreparer, config)
- val managementRoute = new ManagementRoute(deploymentService)
- val processRoute = new ProcessRoute(deploymentService)
- }