Skip to content

Commit

Permalink
Exposes the AkkaBaker config to allow for use of this in the dashboar…
Browse files Browse the repository at this point in the history
…ds. (#1792)

Expose some shared vals & methods in the Http4sBakerServer to allow for re-use them in other places.

Co-authored-by: xk00lj <[email protected]>
  • Loading branch information
Tim-Linschoten and xk00lj authored Oct 22, 2024
1 parent 61829fb commit 43fdd35
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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] =
Expand Down Expand Up @@ -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))
}
}

}

0 comments on commit 43fdd35

Please sign in to comment.