Skip to content

Commit

Permalink
Rename StateStorage => StateRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Mar 9, 2024
1 parent 72f7eee commit 579b8f5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
6 changes: 3 additions & 3 deletions app/src/main/scala/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/scala/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ object AppConfig:
HttpServerConfig.config,
KamonConfig.config,
ExecutorConfg.config,
StorageConfg.config
RepositoryConfig.config
).parMapN(AppConfig.apply)

case class AppConfig(
redis: RedisConfig,
server: HttpServerConfig,
kamon: KamonConfig,
executor: ExecutorConfig,
storage: StorageConfig
repository: RepositoryConfig
)

case class HttpServerConfig(host: Host, port: Port, apiLogger: Boolean)
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/src/test/scala/Helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/src/test/scala/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down

0 comments on commit 579b8f5

Please sign in to comment.