Skip to content

Commit

Permalink
Merge pull request #144 from ScalaConsultants/refactor-configuration
Browse files Browse the repository at this point in the history
Refactor configuration and server startup
  • Loading branch information
jczuchnowski authored Apr 27, 2023
2 parents db6424d + a2fda07 commit e41f32f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/main/g8/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
api {
port = "8080"
host = "0.0.0.0"
port = 8080
}

db {
dataSource {
user=postgres
databaseName=quickstart-db
password=postgres
databaseName=items
password=12345
portNumber=5432
serverName=127.0.0.1
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/g8/src/main/scala/$package$/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package $package$

import $package$.api._
import $package$.api.healthcheck._
import $package$.config.Configuration._
import $package$.config.Configuration.ApiConfig
import $package$.infrastructure._
import io.getquill.jdbczio.Quill
import io.getquill.Literal
Expand All @@ -25,9 +25,9 @@ object Boot extends ZIOAppDefault:

private val serverLayer =
ZLayer
.service[ServerConfig]
.service[ApiConfig]
.flatMap { cfg =>
Server.defaultWithPort(cfg.get.port)
Server.defaultWith(_.binding(cfg.get.host, cfg.get.port))
}
.orDie

Expand All @@ -39,7 +39,7 @@ object Boot extends ZIOAppDefault:
program.provide(
healthCheckServiceLayer,
serverLayer,
ServerConfig.layer,
ApiConfig.layer,
repoLayer,
postgresLayer,
dataSourceLayer,
Expand Down
11 changes: 5 additions & 6 deletions src/main/g8/src/main/scala/$package$/config/Configuration.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package $package$.config

import $package$.config.Configuration.ServerConfig
import com.typesafe.config.ConfigFactory
import zio._
import zio.config._
import zio.config.ConfigDescriptor._
import zio.config.ConfigSource._
import zio.config.typesafe.TypesafeConfigSource

object Configuration:

final case class ServerConfig(port: Int)
final case class ApiConfig(host: String, port: Int)

object ServerConfig:
object ApiConfig:

private val serverConfigDescription =
nested("api") {
int("port").default(8090)
}.to[ServerConfig]
string("host") <*>
int("port")
}.to[ApiConfig]

val layer = ZLayer(
read(
Expand Down

0 comments on commit e41f32f

Please sign in to comment.