From 4f70f12ae66dc93a030112c533b0232d4b7c351e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dragutin=20Marjanovi=C4=87?= Date: Thu, 15 Feb 2024 09:58:41 +0100 Subject: [PATCH] (example): Update zio-config to use standard approach --- modules/example/src/main/scala/example/Main.scala | 6 +++++- .../src/main/scala/example/config/AppConfig.scala | 11 +++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/example/src/main/scala/example/Main.scala b/modules/example/src/main/scala/example/Main.scala index fd9952813..e571c84fb 100644 --- a/modules/example/src/main/scala/example/Main.scala +++ b/modules/example/src/main/scala/example/Main.scala @@ -22,6 +22,7 @@ import example.external.github.RepoFetcher import sttp.client3.SttpBackend import sttp.client3.httpclient.zio.HttpClientZioBackend import zio._ +import zio.config.typesafe.TypesafeConfigProvider import zio.elasticsearch.{ElasticConfig, ElasticExecutor, ElasticRequest, Elasticsearch} import zio.http.{Server, ServerConfig} @@ -30,7 +31,10 @@ import scala.util.Using object Main extends ZIOAppDefault { - override def run: Task[ExitCode] = { + override val bootstrap: Layer[Nothing, Unit] = + Runtime.setConfigProvider(TypesafeConfigProvider.fromResourcePath()) + + def run: Task[ExitCode] = { val elasticConfigLive = ZLayer(ZIO.serviceWith[ElasticsearchConfig](es => ElasticConfig(es.host, es.port))) (prepare *> runServer).provide( diff --git a/modules/example/src/main/scala/example/config/AppConfig.scala b/modules/example/src/main/scala/example/config/AppConfig.scala index da85172ea..937a9f133 100644 --- a/modules/example/src/main/scala/example/config/AppConfig.scala +++ b/modules/example/src/main/scala/example/config/AppConfig.scala @@ -17,14 +17,13 @@ package example.config import zio.config.magnolia.deriveConfig -import zio.config.typesafe.TypesafeConfigProvider -import zio.{Config, Layer, ZLayer} +import zio.{Config, Layer, ZIO, ZLayer} final case class AppConfig(http: HttpConfig, elasticsearch: ElasticsearchConfig) object AppConfig { - lazy val live: Layer[Config.Error, ElasticsearchConfig with HttpConfig] = { - val config = TypesafeConfigProvider.fromResourcePath().load(deriveConfig[AppConfig]) - ZLayer.fromZIO(config.map(_.elasticsearch)) ++ ZLayer.fromZIO(config.map(_.http)) - } + private[this] final val config = ZIO.config(deriveConfig[AppConfig]) + + lazy val live: Layer[Config.Error, ElasticsearchConfig with HttpConfig] = + ZLayer(config.map(_.elasticsearch)) ++ ZLayer(config.map(_.http)) }