Skip to content

Instantly share code, notes, and snippets.

Adam Warski adamw

View GitHub Profile
View app3.scala
import zio._
object Main extends App {
override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] = {
// instead of a method accessor, explicitly accessing the environment
val program: ZIO[Has[UserRegistration], Throwable, User] =
ZIO.accessM[Has[UserRegistration]](_.get.register(User("adam", "adam@hello.com")))
// the DB service is created using through layers (which wrap managed resources)
val dbLayer: ZLayer[Any, Throwable, DB] =
View app2.scala
import zio._
object Main extends App {
override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] = {
ConnectionPoolIntegration
.managedConnectionPool(DBConfig("jdbc://localhost"))
.use { cp =>
lazy val db = new RelationalDB(cp)
lazy val userModel = new DefaultUserModel(db)
lazy val userRegistration = new UserRegistration(DefaultUserNotifier, userModel)
View service4.scala
import zio.Task
trait DB {
def execute(sql: String): Task[Unit]
}
class RelationalDB(cp: ConnectionPool) extends DB {
override def execute(sql: String): Task[Unit] =
Task {
println(s"Running: $sql, on: $cp")
View app1.scala
import zio._
object Main extends App {
override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] = {
// using the UserRegistration's method accessor to construct the program,
// outside of a layer definition
val program: ZIO[UserRegistration, Throwable, User] =
UserRegistration.register(User("adam", "adam@hello.world"))
// composing layers to create a DB instance
View service3.scala
import zio.{Task, ZIO, ZLayer, ZManaged}
object DB {
// 1. service
trait Service {
def execute(sql: String): Task[Unit]
}
// 2. layer
val liveRelationalDB: ZLayer[HasConnectionPool, Throwable, DB] = ZLayer.fromService
View service2.scala
import zio.{Task, ZIO, ZLayer}
object UserRegistration {
// 1. service
class Service(notifier: UserNotifier.Service, userModel: UserModel.Service) {
def register(u: User): Task[User] = {
for {
_ <- userModel.insert(u)
_ <- notifier.notify(u, "Welcome!")
} yield u
View service1.scala
// UserModel.scala
import zio.{Task, ZIO, ZLayer}
object DB {
// 1. service
trait Service {
def execute(sql: String): Task[Unit]
}
// 2., 3. omitted here
View test.scala
package sttp.tapir.examples
import cats.arrow.FunctionK
import cats.data.OptionT
import izumi.reflect.Tag
import org.http4s._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import zio.interop.catz._
View test.scala
package sttp.tapir.examples
import org.http4s._
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import zio.interop.catz._
import zio.{Has, RIO, Runtime, ZIO, ZLayer}
import org.http4s.dsl.Http4sDsl
import zio.clock.Clock
View Test.java
package com.softwaremill.test;
import akka.actor.ActorSystem;
import akka.kafka.CommitterSettings;
import akka.kafka.ConsumerMessage;
import akka.kafka.ConsumerSettings;
import akka.kafka.Subscriptions;
import akka.kafka.javadsl.Committer;
import akka.kafka.javadsl.Consumer;
import akka.stream.OverflowStrategy;
You can’t perform that action at this time.