This file contains 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
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] = |
This file contains 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
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) |
This file contains 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
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") |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
// UserModel.scala | |
import zio.{Task, ZIO, ZLayer} | |
object DB { | |
// 1. service | |
trait Service { | |
def execute(sql: String): Task[Unit] | |
} | |
// 2., 3. omitted here |
This file contains 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
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._ |
This file contains 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
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 |
This file contains 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
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; |