Skip to content

Commit

Permalink
Use refinement type for config (#295)
Browse files Browse the repository at this point in the history
close #40 for real
  • Loading branch information
lenguyenthanh authored Feb 23, 2025
2 parents 36762c3 + 0eb3479 commit 8167e54
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ lazy val backend = (project in file("modules/backend"))
http4sEmberClient,
cirisCore,
cirisHtt4s,
ironCiris,
logback,
scalacheckFaker
),
Expand Down
34 changes: 15 additions & 19 deletions modules/backend/src/main/scala/app.config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import cats.syntax.all.*
import ciris.*
import ciris.http4s.*
import com.comcast.ip4s.*
import fide.types.PositiveInt
import io.github.iltotore.iron.*
import io.github.iltotore.iron.ciris.given
import io.github.iltotore.iron.constraint.all.*

object AppConfig:

Expand All @@ -24,26 +28,28 @@ case class AppConfig(
postgres: db.PostgresConfig
)

case class HttpServerConfig(host: Host, port: Port, shutdownTimeout: Int)
case class HttpServerConfig(host: Host, port: Port, shutdownTimeout: PositiveInt)

object HttpServerConfig:
private def host = env("HTTP_HOST").or(prop("http.host")).as[Host].default(ip"0.0.0.0")
private def port = env("HTTP_PORT").or(prop("http.port")).as[Port].default(port"9669")
private def shutdownTimeout =
env("HTTP_SHUTDOWN_TIMEOUT").or(prop("http.shutdown.timeout")).as[Int].default(30)
env("HTTP_SHUTDOWN_TIMEOUT").or(prop("http.shutdown.timeout")).as[PositiveInt].default(30)
def config = (host, port, shutdownTimeout).parMapN(HttpServerConfig.apply)

case class CrawlerJobConfig(delayInSeconds: Int, intervalInMinutes: Int)
case class CrawlerJobConfig(delayInSeconds: Int :| Positive0, intervalInMinutes: PositiveInt)
object CrawlerJobConfig:
private def delayInSeconds = env("CRAWLER_JOB_DELAY").or(prop("crawler.job.delay")).as[Int].default(3)
private def delayInSeconds =
env("CRAWLER_JOB_DELAY").or(prop("crawler.job.delay")).as[Int :| Positive0].default(3)
private def intervalInMinutes =
env("CRAWLER_JOB_INTERVAL").or(prop("crawler.job.interval")).as[Int].default(60)
env("CRAWLER_JOB_INTERVAL").or(prop("crawler.job.interval")).as[PositiveInt].default(60)
def config = (delayInSeconds, intervalInMinutes).parMapN(CrawlerJobConfig.apply)

object CrawlerConfig:
private def chunkSize = env("CRAWLER_CHUNK_SIZE").or(prop("crawler.chunk.size")).as[Int].default(100)
private def chunkSize =
env("CRAWLER_CHUNK_SIZE").or(prop("crawler.chunk.size")).as[PositiveInt].default(100)
private def concurrentUpsert =
env("CRAWLER_CONCURRENT_UPSERT").or(prop("crawler.concurrent.upsert")).as[Int].default(40)
env("CRAWLER_CONCURRENT_UPSERT").or(prop("crawler.concurrent.upsert")).as[PositiveInt].default(40)
def config = (chunkSize, concurrentUpsert).parMapN(crawler.CrawlerConfig.apply)

object PostgresConfig:
Expand All @@ -54,20 +60,10 @@ object PostgresConfig:
private def password = env("POSTGRES_PASSWORD").or(prop("postgres.password")).as[String]
private def database = env("POSTGRES_DATABASE").or(prop("postgres.database")).as[String]
// max concurrent sessions
private def max = env("POSTGRES_MAX").or(prop("postgres.max")).as[Int]
private def max = env("POSTGRES_MAX").or(prop("postgres.max")).as[PositiveInt]
private def schema = env("POSTGRES_SCHEMA").or(prop("postgres.schema")).as[String].default("fide")
private def debug = env("POSTGRES_DEBUG").or(prop("postgres.debug")).as[Boolean].default(false)
private def ssl = env("POSTGRES_SSL").or(prop("postgres.ssl")).as[Boolean].default(false)

def config =
(
host,
port,
user,
password,
database,
max,
schema,
debug,
ssl
).parMapN(db.PostgresConfig.apply)
(host, port, user, password, database, max, schema, debug, ssl).parMapN(db.PostgresConfig.apply)
1 change: 1 addition & 0 deletions modules/backend/src/test/scala/DbIntegrationSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fide.db.test.Containers
import fide.domain.Federation
import fide.domain.Models.*
import fide.types.PlayerId
import io.github.iltotore.iron.*
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
import weaver.*
Expand Down
3 changes: 2 additions & 1 deletion modules/db/src/main/scala/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package db

import cats.syntax.all.*
import com.comcast.ip4s.*
import fide.types.PositiveInt

case class PostgresConfig(
host: Host,
port: Port,
user: String,
password: String,
database: String,
max: Int,
max: PositiveInt,
schema: String,
debug: Boolean,
ssl: Boolean = false
Expand Down
1 change: 1 addition & 0 deletions modules/db/src/test/scala/Containers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cats.effect.IO
import cats.effect.kernel.Resource
import com.comcast.ip4s.*
import com.dimafeng.testcontainers.PostgreSQLContainer
import io.github.iltotore.iron.*
import org.testcontainers.utility.DockerImageName
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ object Dependencies {
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.4" % Test
val catsEffectTestKit = "org.typelevel" %% "cats-effect-testkit" % V.catsEffect % Test
val scalacheck = "org.scalacheck" %% "scalacheck" % "1.17.0" % Test
val scalacheckFaker = "io.github.etspaceman" %% "scalacheck-faker" % "8.0.6" % Test
val scalacheckFaker = "io.github.etspaceman" %% "scalacheck-faker" % "8.0.6" % Test
}

0 comments on commit 8167e54

Please sign in to comment.