From 6d954bdacecc80fa1389ad2591041ca940549442 Mon Sep 17 00:00:00 2001 From: xk00lj Date: Tue, 22 Oct 2024 10:15:07 +0200 Subject: [PATCH] Exposes the AkkaBaker config to allow for use of this in the dashboards. Expose some shared vals & methods in the Http4sBakerServer to allow for re-use them in other places. --- .../scala/com/ing/bakery/AkkaBakery.scala | 2 +- .../scala/com/ing/bakery/ClosableBakery.scala | 3 +- .../ing/baker/runtime/akka/AkkaBaker.scala | 5 +++ .../server/scaladsl/Http4sBakerServer.scala | 37 +++++++++---------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/bakery/state/src/main/scala/com/ing/bakery/AkkaBakery.scala b/bakery/state/src/main/scala/com/ing/bakery/AkkaBakery.scala index afc50714b..dff300a3d 100644 --- a/bakery/state/src/main/scala/com/ing/bakery/AkkaBakery.scala +++ b/bakery/state/src/main/scala/com/ing/bakery/AkkaBakery.scala @@ -16,7 +16,7 @@ import io.prometheus.client.CollectorRegistry import scala.concurrent.ExecutionContext -case class AkkaBakery(baker: Baker, system: ActorSystem) { +case class AkkaBakery(baker: AkkaBaker, system: ActorSystem) { def executionContext: ExecutionContext = system.dispatcher } diff --git a/bakery/state/src/main/scala/com/ing/bakery/ClosableBakery.scala b/bakery/state/src/main/scala/com/ing/bakery/ClosableBakery.scala index 19a334d83..efce36d8f 100644 --- a/bakery/state/src/main/scala/com/ing/bakery/ClosableBakery.scala +++ b/bakery/state/src/main/scala/com/ing/bakery/ClosableBakery.scala @@ -2,6 +2,7 @@ package com.ing.bakery import akka.actor.ActorSystem import cats.effect.IO +import com.ing.baker.runtime.akka.AkkaBaker import com.ing.baker.runtime.model.InteractionManager import com.ing.baker.runtime.recipe_manager.RecipeManager import com.ing.baker.runtime.scaladsl.Baker @@ -10,7 +11,7 @@ import com.typesafe.config.Config import java.io.Closeable -class ClosableBakery(baker: Baker, +class ClosableBakery(baker: AkkaBaker, system: ActorSystem, close: IO[Unit] ) extends AkkaBakery(baker, system) with Closeable { diff --git a/core/akka-runtime/src/main/scala/com/ing/baker/runtime/akka/AkkaBaker.scala b/core/akka-runtime/src/main/scala/com/ing/baker/runtime/akka/AkkaBaker.scala index 1f11ddf83..6e8a55513 100644 --- a/core/akka-runtime/src/main/scala/com/ing/baker/runtime/akka/AkkaBaker.scala +++ b/core/akka-runtime/src/main/scala/com/ing/baker/runtime/akka/AkkaBaker.scala @@ -78,6 +78,11 @@ object AkkaBaker { class AkkaBaker private[runtime](config: AkkaBakerConfig) extends scaladsl.Baker with LazyLogging { import config.system + /** + * Useful for users that want to directly use any of the AkkaBaker components + */ + def getAkkaBakerConfig: AkkaBakerConfig = config + config.bakerActorProvider.initialize(system) private val recipeManager: RecipeManager = diff --git a/http/baker-http-server/src/main/scala/com/ing/baker/http/server/scaladsl/Http4sBakerServer.scala b/http/baker-http-server/src/main/scala/com/ing/baker/http/server/scaladsl/Http4sBakerServer.scala index 95570cacd..35eae78a6 100644 --- a/http/baker-http-server/src/main/scala/com/ing/baker/http/server/scaladsl/Http4sBakerServer.scala +++ b/http/baker-http-server/src/main/scala/com/ing/baker/http/server/scaladsl/Http4sBakerServer.scala @@ -140,6 +140,23 @@ object Http4sBakerServer extends LazyLogging { OptionT.fromOption(Dashboard.safeGetResourcePath(filename))(sync) .flatMap(resourcePath => StaticFile.fromResource(resourcePath, blocker, Some(request))) } + + implicit val eventInstanceDecoder: EntityDecoder[IO, EventInstance] = jsonOf[IO, EventInstance] + implicit val interactionExecutionRequestDecoder: EntityDecoder[IO, InteractionExecution.ExecutionRequest] = jsonOf[IO, InteractionExecution.ExecutionRequest] + implicit val bakerResultEntityEncoder: EntityEncoder[IO, BakerResult] = jsonEncoderOf[IO, BakerResult] + implicit val recipeDecoder: EntityDecoder[IO, EncodedRecipe] = jsonOf[IO, EncodedRecipe] + + implicit class BakerResultIOHelper[A](io: => IO[A]) { + def toBakerResultResponseIO(implicit encoder: Encoder[A]): IO[Response[IO]] = + io.attempt.flatMap { + case Left(e: BakerException) => Ok(BakerResult(e)) + case Left(e) => + logger.error(s"Unexpected exception happened when calling Baker", e) + InternalServerError(s"No other exception but BakerExceptions should be thrown here: ${e.getCause}") + case Right(()) => Ok(BakerResult.Ack) + case Right(a) => Ok(BakerResult(a)) + } + } } final class Http4sBakerServer private(baker: Baker)(implicit cs: ContextShift[IO]) extends LazyLogging { @@ -158,11 +175,7 @@ final class Http4sBakerServer private(baker: Baker)(implicit cs: ContextShift[IO private object IngredientName extends RegExpValidator("[A-Za-z0-9_]+") - implicit val recipeDecoder: EntityDecoder[IO, EncodedRecipe] = jsonOf[IO, EncodedRecipe] - - implicit val eventInstanceDecoder: EntityDecoder[IO, EventInstance] = jsonOf[IO, EventInstance] - implicit val interactionExecutionRequestDecoder: EntityDecoder[IO, InteractionExecution.ExecutionRequest] = jsonOf[IO, InteractionExecution.ExecutionRequest] - implicit val bakerResultEntityEncoder: EntityEncoder[IO, BakerResult] = jsonEncoderOf[IO, BakerResult] + import Http4sBakerServer._ def routesWithPrefixAndMetrics(apiUrlPrefix: String, metrics: MetricsOps[IO]) (implicit timer: Timer[IO]): HttpRoutes[IO] = @@ -265,22 +278,8 @@ final class Http4sBakerServer private(baker: Baker)(implicit cs: ContextShift[IO } else None } - private implicit class BakerResultFutureHelper[A](f: => Future[A]) { def toBakerResultResponseIO(implicit encoder: Encoder[A]): IO[Response[IO]] = IO.fromFuture(IO(f)).toBakerResultResponseIO } - - private implicit class BakerResultIOHelper[A](io: => IO[A]) { - def toBakerResultResponseIO(implicit encoder: Encoder[A]): IO[Response[IO]] = - io.attempt.flatMap { - case Left(e: BakerException) => Ok(BakerResult(e)) - case Left(e) => - logger.error(s"Unexpected exception happened when calling Baker", e) - InternalServerError(s"No other exception but BakerExceptions should be thrown here: ${e.getCause}") - case Right(()) => Ok(BakerResult.Ack) - case Right(a) => Ok(BakerResult(a)) - } - } - }