Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.effect.*
- import cats.effect.std.Dispatcher
- import cats.effect.unsafe.Scheduler
- import cats.syntax.all.*
- class BackendModule[F[_]: Async] extends Module.Independent[F, DbConfig, Api[F]]:
- def loadConfig: Resource[F, DbConfig] = configAt[DbConfig]("db")
- def loadApi(config: DbConfig): Resource[F, Api[F]] =
- DbManager.make[F](config).setupDb.map { xa =>
- Api(DishDao.make[F](xa), MealDao.make[F](xa))
- }
- end BackendModule
- class FrontendModule[F[_]: Async] extends Module[F, Unit, F[Unit], Dependencies[F]]:
- def loadConfig: Resource[F, Unit] = Resource.unit[F]
- def loadApi(config: Unit, dependency: Dependencies[F]): Resource[F, F[Unit]] =
- for {
- dispatcher <- Dispatcher[F]
- given DependencyF[F, DishDao] = dependency._2.map(_.dishes)
- given DependencyF[F, MealDao] = dependency._2.map(_.meals)
- randomizer = Randomizer(dispatcher)
- editor = Editor(dependency._1, dispatcher)
- viewer = DishViewer(dispatcher)
- gui <- AppWindow.show[F](randomizer, editor, viewer)
- } yield gui
- object FrontendModule:
- type Dependencies[F[_]] = (Scheduler, DependencyF[F, backend.Api])
- end FrontendModule
- type DependencyF[F[_], G[_[_]]] = DeferredSource[F, G[F]]
- object App extends HardExitResourceApp:
- override def run(args: List[String]): Resource[IO, ExitCode] =
- val backend = BackendModule[IO]
- val frontend = FrontendModule[IO]
- for {
- config <- backend.loadConfig
- deferred <- backend.deferredApi
- gui <- frontend.loadModule(runtime.scheduler, deferred)
- _ <- backend.initApi(config, deferred)
- _ <- Resource.eval(gui)
- } yield ExitCode.Success
- end run
- end App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement