Skip to content

Instantly share code, notes, and snippets.

View lforite's full-sized avatar
💯
TTT, types types types

Louis Forite lforite

💯
TTT, types types types
View GitHub Profile
@lforite
lforite / authorization_token.scala
Created October 22, 2021 07:40
Scala web series pt1 authorization token example
case class Token(
userId: UUID,
clientId: UUID,
projectIds: List[UUID],
permissions: List[String],
issuedAt: Instant
)
@lforite
lforite / shutdown-hooks.scala
Last active February 26, 2021 13:54
Play shutdown hooks
case class KafkaClient(applicationLifecycle: ApplicationLifecycle) {
val kafkaResource = acquireResource()
applicationLifecycle.addStopHook(() =>
Future {
kafkaResource.close()
}
)
}
case class UserService(client: KafkaClient) {
@lforite
lforite / application.scala
Last active February 26, 2021 14:01
App init with Cats Resource
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(...)) *>
@lforite
lforite / cats_make.scala
Created November 17, 2020 09:25
Cats make signature Scala
// from Cats code source
def make[F[_], A](acquire: F[A])(release: A => F[Unit])(implicit F: Functor[F]): Resource[F, A]
@lforite
lforite / create_kafka_client.scala
Last active February 26, 2021 14:02
Creating a kafka client using Cats Resource
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)))
Resource.make {
IO(logger.info("Creating gRPC server")) *>
IO {
ServerBuilder
.forPort(8081)
.addService(userService)
.build()
}
}(grpcServer =>
IO(logger.info("Stopping gRPC server...")) *>
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")
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]
}
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)
case class Kleisli[F[_], A, B](run: A => F[B])