Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active June 4, 2020 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/71732b86ecf89689d0cabcabca28f615 to your computer and use it in GitHub Desktop.
Save adamw/71732b86ecf89689d0cabcabca28f615 to your computer and use it in GitHub Desktop.
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
val dbLayer: ZLayer[Any, Throwable, DB] =
ZLayer.succeed(DBConfig("jdbc://localhost")) >>>
ConnectionPoolIntegration.live >>>
DB.liveRelationalDB
// composing layers to create a UserRegistration instance
val userRegistrationLayer: ZLayer[Any, Throwable, UserRegistration] =
((dbLayer >>> UserModel.live) ++ UserNotifier.live) >>> UserRegistration.live
// creating the complete application description
program
.provideLayer(userRegistrationLayer)
.catchAll(t => ZIO.succeed(t.printStackTrace()).map(_ => ExitCode.failure))
.map { u =>
println(s"Registered user: $u (layers)")
ExitCode.success
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment