From a6883727f9647a74cd306c1cf74f06f4257cf492 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Sat, 20 Jul 2024 23:03:45 +0900 Subject: [PATCH] refactor: better scope --- .../scala/net/yoshinorin/qualtet/BootStrap.scala | 5 ++--- src/main/scala/net/yoshinorin/qualtet/Modules.scala | 5 +++-- .../scala/net/yoshinorin/qualtet/auth/JwtSpec.scala | 2 +- .../net/yoshinorin/qualtet/config/ConfigSpec.scala | 2 +- .../net/yoshinorin/qualtet/fixture/Fixture.scala | 12 +++++++----- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/scala/net/yoshinorin/qualtet/BootStrap.scala b/src/main/scala/net/yoshinorin/qualtet/BootStrap.scala index 814beca0..f24aad42 100644 --- a/src/main/scala/net/yoshinorin/qualtet/BootStrap.scala +++ b/src/main/scala/net/yoshinorin/qualtet/BootStrap.scala @@ -30,11 +30,10 @@ object BootStrap extends IOApp { } def run(args: List[String]): IO[ExitCode] = { - val host = Ipv4Address.fromString(Modules.config.http.host).getOrElse(ipv4"127.0.0.1") - val port = Port.fromInt(Modules.config.http.port).getOrElse(port"9001") - Modules.transactorResource.use { tx => val modules = new Modules(tx) + val host = Ipv4Address.fromString(modules.config.http.host).getOrElse(ipv4"127.0.0.1") + val port = Port.fromInt(modules.config.http.port).getOrElse(port"9001") (for { _ <- logger.info(ApplicationInfo.asJson) _ <- IO(modules.migrator.migrate(modules.contentTypeService)) diff --git a/src/main/scala/net/yoshinorin/qualtet/Modules.scala b/src/main/scala/net/yoshinorin/qualtet/Modules.scala index 606c4e25..eacebd7e 100644 --- a/src/main/scala/net/yoshinorin/qualtet/Modules.scala +++ b/src/main/scala/net/yoshinorin/qualtet/Modules.scala @@ -53,8 +53,9 @@ import java.security.SecureRandom import java.util.concurrent.TimeUnit object Modules { - val config = ApplicationConfig.load - val doobieTransactor: DoobieTransactor[Aux] = summon[DoobieTransactor[Aux]] + private val config = ApplicationConfig.load + private val doobieTransactor: DoobieTransactor[Aux] = summon[DoobieTransactor[Aux]] + val transactorResource = doobieTransactor.make(config.db) given log4catsLogger: Log4CatsLoggerFactory[IO] = Log4CatsSlf4jFactory.create[IO] } diff --git a/src/test/scala/net/yoshinorin/qualtet/auth/JwtSpec.scala b/src/test/scala/net/yoshinorin/qualtet/auth/JwtSpec.scala index 96d9d57e..b1d8f728 100644 --- a/src/test/scala/net/yoshinorin/qualtet/auth/JwtSpec.scala +++ b/src/test/scala/net/yoshinorin/qualtet/auth/JwtSpec.scala @@ -20,7 +20,7 @@ import java.time.Instant class JwtSpec extends AnyWordSpec { val mod = Modules(fixtureTx) - val config = Modules.config + val config = mod.config val jc: JwtClaim = JwtClaim( iss = config.jwt.iss, aud = config.jwt.aud, diff --git a/src/test/scala/net/yoshinorin/qualtet/config/ConfigSpec.scala b/src/test/scala/net/yoshinorin/qualtet/config/ConfigSpec.scala index a138f7d4..2caab16d 100644 --- a/src/test/scala/net/yoshinorin/qualtet/config/ConfigSpec.scala +++ b/src/test/scala/net/yoshinorin/qualtet/config/ConfigSpec.scala @@ -1,6 +1,6 @@ package net.yoshinorin.qualtet.config -import net.yoshinorin.qualtet.Modules.* +import net.yoshinorin.qualtet.fixture.Fixture.config import org.scalatest.wordspec.AnyWordSpec // testOnly net.yoshinorin.qualtet.config.ConfigSpec diff --git a/src/test/scala/net/yoshinorin/qualtet/fixture/Fixture.scala b/src/test/scala/net/yoshinorin/qualtet/fixture/Fixture.scala index 363b925d..6e1b59bf 100644 --- a/src/test/scala/net/yoshinorin/qualtet/fixture/Fixture.scala +++ b/src/test/scala/net/yoshinorin/qualtet/fixture/Fixture.scala @@ -11,6 +11,7 @@ import org.typelevel.log4cats.slf4j.{Slf4jFactory => Log4CatsSlf4jFactory} import com.github.benmanes.caffeine.cache.Caffeine import com.github.benmanes.caffeine.cache.{Cache => CaffeineCache} import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec +import net.yoshinorin.qualtet.config.ApplicationConfig import net.yoshinorin.qualtet.http.AuthProvider import net.yoshinorin.qualtet.http.CorsProvider import net.yoshinorin.qualtet.cache.CacheModule @@ -53,15 +54,16 @@ object Fixture { ULID.newULIDString.toLower } - val h: String = Modules.config.http.host - val p: String = Modules.config.http.port.toString() + val config = ApplicationConfig.load + val h: String = config.http.host + val p: String = config.http.port.toString() val host = Uri.unsafeFromString(s"http://${h}:${p}") val fixtureTx = Transactor.fromDriverManager[IO]( driver = "org.mariadb.jdbc.Driver", - url = Modules.config.db.url, - user = Modules.config.db.user, - password = Modules.config.db.password, + url = config.db.url, + user = config.db.user, + password = config.db.password, logHandler = None ) private val modules = Modules(fixtureTx)