Skip to content

Commit

Permalink
Merge pull request #45 from geirolz/Add_fly4s_support
Browse files Browse the repository at this point in the history
Add Fly4s module
  • Loading branch information
geirolz authored Jun 30, 2023
2 parents 29545e6 + 5e61b8f commit 59ff1cb
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 49 deletions.
79 changes: 70 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ Given

```scala
import cats.Show
import cats.effect.{ExitCode, Resource, IO, IOApp}
import cats.effect.{Resource, IO}
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.logger.ToolkitLogger
import com.geirolz.app.toolkit.novalues.NoResources
import org.typelevel.log4cats.slf4j.Slf4jLogger

// Define config
case class Config(host: String, port: Int)
Expand All @@ -39,9 +38,7 @@ object Config {
}

// Define service dependencies
case class AppDependencyServices(
kafkaConsumer: KafkaConsumer[IO]
)
case class AppDependencyServices(kafkaConsumer: KafkaConsumer[IO])

object AppDependencyServices {
def resource(res: App.Resources[SimpleAppInfo[String], ToolkitLogger[IO], Config, NoResources]): Resource[IO, AppDependencyServices] =
Expand All @@ -67,11 +64,12 @@ object KafkaConsumer {
}
```

You can write your app as

```scala
import cats.effect.{ExitCode, IO, IOApp}
import com.geirolz.app.toolkit.App
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.logger.ToolkitLogger
import com.geirolz.app.toolkit.error.*

object Main extends IOApp {
override def run(args: List[String]): IO[ExitCode] =
Expand All @@ -95,7 +93,7 @@ object Main extends IOApp {
.compile
.drain
)
.beforeRun(_.logger.info("CUSTOM PRE-RUN"))
.beforeProviding(_.logger.info("CUSTOM PRE-PROVIDING"))
.onFinalize(_.logger.info("CUSTOM END"))
.run(args)
}
Expand All @@ -109,11 +107,18 @@ object Main extends IOApp {
libraryDependencies += "com.github.geirolz" %% "toolkit-pureconfig" % "0.0.8"
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`
Import the syntax

```scala
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`

```scala
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.config.pureconfig.syntax.*

case class TestConfig(value: String)
Expand Down Expand Up @@ -149,4 +154,60 @@ libraryDependencies += "com.github.geirolz" %% "toolkit-log4cats" % "0.0.8"

```sbt
libraryDependencies += "com.github.geirolz" %% "toolkit-odin" % "0.0.8"
```

#### fly4s

```sbt
libraryDependencies += "com.github.geirolz" %% "toolkit-fly4s" % "0.0.8"
```

Import the syntax

```scala
import com.geirolz.app.toolkit.fly4s.syntax.*
```

Which allows you to use `beforeProvidingMigrateDatabaseWithConfig` on `App` to migrate the database before running the
app.
To have access to the whole app dependencies you can use `beforeProvidingMigrateDatabaseWith` instead while to have
access to
the whole app dependencies to provide a custom `Fly4s` instance you can use `beforeProvidingMigrateDatabase`.

```scala
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.fly4s.syntax.*
import com.geirolz.app.toolkit.{App, SimpleAppInfo}

case class TestConfig(dbUrl: String, dbUser: Option[String], dbPassword: Option[Array[Char]])

object TestConfig {
implicit val show: Show[TestConfig] = Show.fromToString
}

App[IO]
.withInfo(
SimpleAppInfo.string(
name = "toolkit",
version = "0.0.1",
scalaVersion = "2.13.10",
sbtVersion = "1.8.0"
)
)
.withConfig(
TestConfig(
dbUrl = "jdbc:postgresql://localhost:5432/toolkit",
dbUser = Some("postgres"),
dbPassword = Some("postgres".toCharArray)
)
)
.withoutDependencies
.provideOne(_ => IO.unit)
.beforeProvidingMigrateDatabaseWithConfig(
url = _.dbUrl,
user = _.dbUser,
password = _.dbPassword
)
.run_
```
17 changes: 13 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ lazy val root: Project = project
.settings(
copyReadMe := IO.copyFile(file("docs/compiled/README.md"), file("README.md"))
)
.aggregate(core, docs, config, testing, log4cats, odin, pureconfig)
.aggregate(core, docs, config, testing, log4cats, odin, pureconfig, fly4s)

lazy val docs: Project =
project
.in(file("docs"))
.enablePlugins(MdocPlugin)
.dependsOn(core, config, log4cats, odin, pureconfig)
.dependsOn(core, config, log4cats, odin, pureconfig, fly4s)
.settings(
baseSettings,
noPublishSettings,
Expand Down Expand Up @@ -142,13 +142,22 @@ lazy val odin: Project =
libraryDependencies ++= ProjectDependencies.Integrations.Odin.dedicated
)

lazy val `pureconfig`: Project =
lazy val pureconfig: Project =
module("pureconfig")(
folder = s"$integrationsFolder/pureconfig",
publishAs = Some(subProjectName("pureconfig"))
).dependsOn(core, config)
.settings(
libraryDependencies ++= ProjectDependencies.Integrations.ConfigPureConfig.dedicated
libraryDependencies ++= ProjectDependencies.Integrations.Pureconfig.dedicated
)

lazy val fly4s: Project =
module("fly4s")(
folder = s"$integrationsFolder/fly4s",
publishAs = Some(subProjectName("fly4s"))
).dependsOn(core)
.settings(
libraryDependencies ++= ProjectDependencies.Integrations.Fly4s.dedicated
)

//=============================== MODULES UTILS ===============================
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/scala/com/geirolz/app/toolkit/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class App[
val loggerBuilder: F[LOGGER_T[F]],
val configLoader: F[CONFIG],
val resourcesLoader: F[RESOURCES],
val beforeRunF: App.Dependencies[APP_INFO, LOGGER_T[F], CONFIG, DEPENDENCIES, RESOURCES] => F[Unit],
val beforeProvidingF: App.Dependencies[APP_INFO, LOGGER_T[F], CONFIG, DEPENDENCIES, RESOURCES] => F[Unit],
val onFinalizeF: App.Dependencies[APP_INFO, LOGGER_T[F], CONFIG, DEPENDENCIES, RESOURCES] => F[Unit],
val failureHandlerLoader: App.Resources[APP_INFO, LOGGER_T[F], CONFIG, RESOURCES] => FailureHandler[F, FAILURE],
val dependenciesLoader: App.Resources[APP_INFO, LOGGER_T[F], CONFIG, RESOURCES] => Resource[F, FAILURE \/ DEPENDENCIES],
Expand Down Expand Up @@ -60,15 +60,15 @@ class App[
def withMessages(messages: AppMessages): Self =
copyWith(appMessages = messages)

def beforeRun(
def beforeProviding(
f: App.Dependencies[APP_INFO, LOGGER_T[F], CONFIG, DEPENDENCIES, RESOURCES] => F[Unit]
): Self =
copyWith(beforeRunF = f)
copyWith(beforeProvidingF = beforeProvidingF >> f)

def onFinalize(
f: App.Dependencies[APP_INFO, LOGGER_T[F], CONFIG, DEPENDENCIES, RESOURCES] => F[Unit]
): Self =
copyWith(onFinalizeF = f)
copyWith(onFinalizeF = onFinalizeF >> f)

private[toolkit] def _compile(appArgs: List[String]): Resource[F, FAILURE \/ F[NonEmptyList[FAILURE] \/ Unit]] =
(
Expand Down Expand Up @@ -146,7 +146,7 @@ class App[
} yield maybeReducedFailures.toLeft(())
} yield {
appLoggerF.info(appMessages.startingApp) >>
beforeRunF(appDependencies) >>
beforeProvidingF(appDependencies) >>
appLogic
.onCancel(appLoggerF.info(appMessages.appWasStopped))
.onError(e => appLoggerF.error(e)(appMessages.appEnErrorOccurred))
Expand Down Expand Up @@ -180,13 +180,13 @@ class App[
RES2,
DEPS2
](
appInfo: APP_INFO2 = this.appInfo,
appMessages: AppMessages = this.appMessages,
loggerBuilder: G[LOGGER_T2[G]] = this.loggerBuilder,
configLoader: G[CONFIG2] = this.configLoader,
resourcesLoader: G[RES2] = this.resourcesLoader,
beforeRunF: App.Dependencies[APP_INFO2, LOGGER_T2[G], CONFIG2, DEPS2, RES2] => G[Unit] = this.beforeRunF,
onFinalizeF: App.Dependencies[APP_INFO2, LOGGER_T2[G], CONFIG2, DEPS2, RES2] => G[Unit] = this.onFinalizeF,
appInfo: APP_INFO2 = this.appInfo,
appMessages: AppMessages = this.appMessages,
loggerBuilder: G[LOGGER_T2[G]] = this.loggerBuilder,
configLoader: G[CONFIG2] = this.configLoader,
resourcesLoader: G[RES2] = this.resourcesLoader,
beforeProvidingF: App.Dependencies[APP_INFO2, LOGGER_T2[G], CONFIG2, DEPS2, RES2] => G[Unit] = this.beforeProvidingF,
onFinalizeF: App.Dependencies[APP_INFO2, LOGGER_T2[G], CONFIG2, DEPS2, RES2] => G[Unit] = this.onFinalizeF,
failureHandlerLoader: App.Resources[APP_INFO2, LOGGER_T2[G], CONFIG2, RES2] => FailureHandler[
G,
FAILURE2
Expand All @@ -205,7 +205,7 @@ class App[
loggerBuilder = loggerBuilder,
configLoader = configLoader,
resourcesLoader = resourcesLoader,
beforeRunF = beforeRunF,
beforeProvidingF = beforeProvidingF,
onFinalizeF = onFinalizeF,
failureHandlerLoader = failureHandlerLoader,
dependenciesLoader = dependenciesLoader,
Expand Down Expand Up @@ -434,7 +434,7 @@ object App extends AppSyntax {
failureHandlerLoader = _ => FailureHandler.cancelAll,
loggerBuilder = loggerBuilder,
resourcesLoader = resourcesLoader,
beforeRunF = _ => ().pure[F],
beforeProvidingF = _ => ().pure[F],
onFinalizeF = _ => ().pure[F],
configLoader = configLoader,
dependenciesLoader = dependenciesLoader,
Expand Down
79 changes: 70 additions & 9 deletions docs/compiled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ Given

```scala
import cats.Show
import cats.effect.{ExitCode, Resource, IO, IOApp}
import cats.effect.{Resource, IO}
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.logger.ToolkitLogger
import com.geirolz.app.toolkit.novalues.NoResources
import org.typelevel.log4cats.slf4j.Slf4jLogger

// Define config
case class Config(host: String, port: Int)
Expand All @@ -39,9 +38,7 @@ object Config {
}

// Define service dependencies
case class AppDependencyServices(
kafkaConsumer: KafkaConsumer[IO]
)
case class AppDependencyServices(kafkaConsumer: KafkaConsumer[IO])

object AppDependencyServices {
def resource(res: App.Resources[SimpleAppInfo[String], ToolkitLogger[IO], Config, NoResources]): Resource[IO, AppDependencyServices] =
Expand All @@ -67,11 +64,12 @@ object KafkaConsumer {
}
```

You can write your app as

```scala
import cats.effect.{ExitCode, IO, IOApp}
import com.geirolz.app.toolkit.App
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.logger.ToolkitLogger
import com.geirolz.app.toolkit.error.*

object Main extends IOApp {
override def run(args: List[String]): IO[ExitCode] =
Expand All @@ -95,7 +93,7 @@ object Main extends IOApp {
.compile
.drain
)
.beforeRun(_.logger.info("CUSTOM PRE-RUN"))
.beforeProviding(_.logger.info("CUSTOM PRE-PROVIDING"))
.onFinalize(_.logger.info("CUSTOM END"))
.run(args)
}
Expand All @@ -109,11 +107,18 @@ object Main extends IOApp {
libraryDependencies += "com.github.geirolz" %% "toolkit-pureconfig" % "0.0.8"
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`
Import the syntax

```scala
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`

```scala
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.config.pureconfig.syntax.*

case class TestConfig(value: String)
Expand Down Expand Up @@ -149,4 +154,60 @@ libraryDependencies += "com.github.geirolz" %% "toolkit-log4cats" % "0.0.8"

```sbt
libraryDependencies += "com.github.geirolz" %% "toolkit-odin" % "0.0.8"
```

#### fly4s

```sbt
libraryDependencies += "com.github.geirolz" %% "toolkit-fly4s" % "0.0.8"
```

Import the syntax

```scala
import com.geirolz.app.toolkit.fly4s.syntax.*
```

Which allows you to use `beforeProvidingMigrateDatabaseWithConfig` on `App` to migrate the database before running the
app.
To have access to the whole app dependencies you can use `beforeProvidingMigrateDatabaseWith` instead while to have
access to
the whole app dependencies to provide a custom `Fly4s` instance you can use `beforeProvidingMigrateDatabase`.

```scala
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.fly4s.syntax.*
import com.geirolz.app.toolkit.{App, SimpleAppInfo}

case class TestConfig(dbUrl: String, dbUser: Option[String], dbPassword: Option[Array[Char]])

object TestConfig {
implicit val show: Show[TestConfig] = Show.fromToString
}

App[IO]
.withInfo(
SimpleAppInfo.string(
name = "toolkit",
version = "0.0.1",
scalaVersion = "2.13.10",
sbtVersion = "1.8.0"
)
)
.withConfig(
TestConfig(
dbUrl = "jdbc:postgresql://localhost:5432/toolkit",
dbUser = Some("postgres"),
dbPassword = Some("postgres".toCharArray)
)
)
.withoutDependencies
.provideOne(_ => IO.unit)
.beforeProvidingMigrateDatabaseWithConfig(
url = _.dbUrl,
user = _.dbUser,
password = _.dbPassword
)
.run_
```
Loading

0 comments on commit 59ff1cb

Please sign in to comment.