From 579b8f5ff10b0feac14b690f31e1f451db6ac287 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Sat, 9 Mar 2024 20:31:09 +0700 Subject: [PATCH] Rename StateStorage => StateRepository --- app/src/main/scala/App.scala | 6 +++--- app/src/main/scala/AppConfig.scala | 10 +++++----- app/src/main/scala/Executor.scala | 6 +++--- ...ateStorage.scala => StateRepository.scala} | 20 +++++++------------ app/src/test/scala/Helper.scala | 4 ++-- app/src/test/scala/IntegrationTest.scala | 2 +- 6 files changed, 21 insertions(+), 27 deletions(-) rename app/src/main/scala/{StateStorage.scala => StateRepository.scala} (77%) diff --git a/app/src/main/scala/App.scala b/app/src/main/scala/App.scala index 97f6882..a368a97 100644 --- a/app/src/main/scala/App.scala +++ b/app/src/main/scala/App.scala @@ -24,9 +24,9 @@ class FishnetApp(res: AppResources, config: AppConfig)(using Logger[IO]): def run(): Resource[IO, Unit] = for lilaClient <- Resource.pure(LilaClient(res.redisPubsub)) - monitor = Monitor.apply - storage = StateStorage.instance(config.storage.path) - executor <- Executor.instance(lilaClient, storage, monitor, config.executor) + monitor = Monitor.apply + repository = StateRepository.instance(config.repository.path) + executor <- Executor.instance(lilaClient, repository, monitor, config.executor) httpApi = HttpApi(executor, HealthCheck(), config.server) server <- MkHttpServer.apply.newEmber(config.server, httpApi.httpApp) _ <- RedisSubscriberJob(executor, res.redisPubsub).run().background diff --git a/app/src/main/scala/AppConfig.scala b/app/src/main/scala/AppConfig.scala index 657cd12..b65c55c 100644 --- a/app/src/main/scala/AppConfig.scala +++ b/app/src/main/scala/AppConfig.scala @@ -15,7 +15,7 @@ object AppConfig: HttpServerConfig.config, KamonConfig.config, ExecutorConfg.config, - StorageConfg.config + RepositoryConfig.config ).parMapN(AppConfig.apply) case class AppConfig( @@ -23,7 +23,7 @@ case class AppConfig( server: HttpServerConfig, kamon: KamonConfig, executor: ExecutorConfig, - storage: StorageConfig + repository: RepositoryConfig ) case class HttpServerConfig(host: Host, port: Port, apiLogger: Boolean) @@ -52,7 +52,7 @@ object ExecutorConfg: def maxSize = env("APP_MAX_MOVE_SIZE").or(prop("app.max.move.size")).as[Int].default(300) def config = maxSize.map(ExecutorConfig.apply) -case class StorageConfig(path: Option[String]) -object StorageConfg: +case class RepositoryConfig(path: Option[String]) +object RepositoryConfig: def path = env("APP_BACKUP_FILE").or(prop("app.backup.file")).as[String].option - def config = path.map(StorageConfig.apply) + def config = path.map(RepositoryConfig.apply) diff --git a/app/src/main/scala/Executor.scala b/app/src/main/scala/Executor.scala index 1ec2122..c9faa51 100644 --- a/app/src/main/scala/Executor.scala +++ b/app/src/main/scala/Executor.scala @@ -28,15 +28,15 @@ object Executor: import AppState.* - def instance(client: LilaClient, storage: StateStorage, monitor: Monitor, config: ExecutorConfig)(using - Logger[IO] + def instance(client: LilaClient, repository: StateRepository, monitor: Monitor, config: ExecutorConfig)( + using Logger[IO] ): Resource[IO, Executor] = Ref .of[IO, AppState](AppState.empty) .toResource .flatMap: ref => Resource - .make(storage.get.flatMap(ref.set))(_ => ref.get.flatMap(storage.save)) + .make(repository.get.flatMap(ref.set))(_ => ref.get.flatMap(repository.save)) .as(instance(ref, client, monitor, config)) def instance(ref: Ref[IO, AppState], client: LilaClient, monitor: Monitor, config: ExecutorConfig)(using diff --git a/app/src/main/scala/StateStorage.scala b/app/src/main/scala/StateRepository.scala similarity index 77% rename from app/src/main/scala/StateStorage.scala rename to app/src/main/scala/StateRepository.scala index 1c5c594..43d4dd3 100644 --- a/app/src/main/scala/StateStorage.scala +++ b/app/src/main/scala/StateRepository.scala @@ -4,30 +4,24 @@ import cats.effect.IO import cats.syntax.all.* import org.typelevel.log4cats.Logger -trait StateStorage: +trait StateRepository: def get: IO[AppState] def save(state: AppState): IO[Unit] -object StateStorage: +object StateRepository: - def inMemory: IO[StateStorage] = - for ref <- cats.effect.Ref.of[IO, AppState](AppState.empty) - yield new StateStorage: - def get: IO[AppState] = ref.get - def save(state: AppState): IO[Unit] = ref.set(state) - - def instance(path: Option[String])(using Logger[IO]): StateStorage = + def instance(path: Option[String])(using Logger[IO]): StateRepository = path.fold(noop)(file(_)) - def noop(using Logger[IO]): StateStorage = - new StateStorage: + def noop(using Logger[IO]): StateRepository = + new StateRepository: def get: IO[AppState] = Logger[IO].info("There is no configed path, return empty AppState") *> IO(AppState.empty) def save(state: AppState): IO[Unit] = Logger[IO].info("There is no configed path, do nothing") - def file(_path: String)(using Logger[IO]): StateStorage = + def file(_path: String)(using Logger[IO]): StateRepository = val path = fs2.io.file.Path(_path) - new StateStorage: + new StateRepository: def get: IO[AppState] = Logger[IO].info(s"Reading state from $path") *> fs2.io.file diff --git a/app/src/test/scala/Helper.scala b/app/src/test/scala/Helper.scala index fbb75d7..e16df79 100644 --- a/app/src/test/scala/Helper.scala +++ b/app/src/test/scala/Helper.scala @@ -13,7 +13,7 @@ object Helper: new LilaClient: def send(move: Lila.Move): IO[Unit] = IO.unit - val noopStateStorage: StateStorage = - new StateStorage: + val noopStateRepository: StateRepository = + new StateRepository: def get: IO[AppState] = IO.pure(AppState.empty) def save(state: AppState): IO[Unit] = IO.unit diff --git a/app/src/test/scala/IntegrationTest.scala b/app/src/test/scala/IntegrationTest.scala index 5ffc390..c534d1e 100644 --- a/app/src/test/scala/IntegrationTest.scala +++ b/app/src/test/scala/IntegrationTest.scala @@ -40,7 +40,7 @@ object IntegrationTest extends IOSuite: redis = redis, kamon = KamonConfig(enabled = false), executor = ExecutorConfig(maxSize = 300), - storage = StorageConfig(path = None) + repository = RepositoryConfig(path = None) ) test("health check should return healthy"):