This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class KafkaClient(applicationLifecycle: ApplicationLifecycle) { | |
| val kafkaResource = acquireResource() | |
| applicationLifecycle.addStopHook(() => | |
| Future { | |
| kafkaResource.close() | |
| } | |
| ) | |
| } | |
| case class UserService(client: KafkaClient) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Application extends IOApp { | |
| override def run(args: List[String]): IO[ExitCode] = { | |
| (for { | |
| kafkaClient <- kafkaClientResource | |
| userService = UserService(kafkaClient) | |
| grpcServer <- grpcServerResource(userService) | |
| } yield (kafkaClient, grpcServer)).use { | |
| case (kafkaClient, grpcServer) => | |
| IO(logger.info("Using both kafkaClient and gRPC server")) *> | |
| IO(kafkaClient.createTopic(...)) *> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from Cats code source | |
| def make[F[_], A](acquire: F[A])(release: A => F[Unit])(implicit F: Functor[F]): Resource[F, A] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Resource.make { | |
| IO(logger.info("Creating a kafka client")) *> | |
| IO { | |
| val properties = new java.util.Properties() | |
| properties.setProperty(ClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092") | |
| KafkaClient.create(properties) | |
| } | |
| }(client => | |
| IO(logger.info("Closing Kafka client...")) *> | |
| IO(client.close(Duration.ofSeconds(10L))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Resource.make { | |
| IO(logger.info("Creating gRPC server")) *> | |
| IO { | |
| ServerBuilder | |
| .forPort(8081) | |
| .addService(userService) | |
| .build() | |
| } | |
| }(grpcServer => | |
| IO(logger.info("Stopping gRPC server...")) *> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Traceable[T] = CorrelationId ?=> T | |
| type RIO[T] = Traceable[Future[T]] | |
| def main(args: Array[String]): Unit = { | |
| given cid: CorrelationId = CorrelationId("correlatidid-123") | |
| val future: RIO[User] = (for { | |
| user <- createUser("hello", "world") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object RIO { | |
| def unit: Kleisli[IO, CorrelationId, Unit] = Kleisli.pure(()) | |
| def liftF[B](x: IO[B]): Kleisli[IO, CorrelationId, B] = Kleisli.liftF[IO, CorrelationId, B](x) | |
| def pure[B](x: B): Kleisli[IO, CorrelationId, B] = Kleisli.pure(x) | |
| def apply[B](f: CorrelationId => IO[B]): Kleisli[IO, CorrelationId, B] = Kleisli(f) | |
| def ask: Kleisli[IO, CorrelationId, CorrelationId] = Kleisli.ask[IO, CorrelationId] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Validated[+E, +A] { | |
| case Invalid[E](e: E) extends Validated[E, Nothing] | |
| case Valid(a: A) extends Validated[Nothing, A] | |
| } | |
| given validatedOps[E, A] : { | |
| def (fa: Validated[E, A]) zip[EE, B](fb: Validated[EE, B]): Validated[E | EE, (A, B)] = | |
| (fa, fb) match | |
| case (Valid(a), Valid(b)) => Valid((a, b)) | |
| case (Invalid(e), _) => Invalid(e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class Kleisli[F[_], A, B](run: A => F[B]) |