Skip to content

Commit

Permalink
Update to ZIO 2 & http4s 0.23.23 (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-kai authored Sep 29, 2023
1 parent f24fc14 commit 1ecc3d0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 36 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,3 @@ It seems like we've managed to build an exhaustive ruleset for `docker-java` so
If something is wrong and you need to generate the rules for `docker-java`, run the agent twice in both hot and cold state.
- Only `PluginConfig.const` works reliably under Native Image. So, ClassGraph analysis is disabled in `ni-filter.json`.
You can't make dynamic plugin resolution working under Native Image.

9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ val V = new {
val logstage = distage
val scalatest = "3.2.17"
val scalacheck = "1.17.0"
val http4s = "0.23.15"
val http4s = "0.23.23"
val doobie = "1.0.0-RC4"
val catsCore = "2.10.0"
val zio = "2.0.18"
val zioCats = "13.0.0.2"
val zioCats = "23.0.0.8"
val kindProjector = "0.13.2"
val circeGeneric = "0.14.6"
val graalMetadata = "0.9.27"
Expand All @@ -25,8 +25,8 @@ val Deps = new {
val logstageSlf4j = "io.7mind.izumi" %% "logstage-adapter-slf4j" % V.logstage

val http4sDsl = "org.http4s" %% "http4s-dsl" % V.http4s
val http4sServer = "org.http4s" %% "http4s-blaze-server" % V.http4s
val http4sClient = "org.http4s" %% "http4s-blaze-client" % V.http4s
val http4sServer = "org.http4s" %% "http4s-ember-server" % V.http4s
val http4sClient = "org.http4s" %% "http4s-ember-client" % V.http4s
val http4sCirce = "org.http4s" %% "http4s-circe" % V.http4s

val circeGeneric = "io.circe" %% "circe-generic" % V.circeGeneric
Expand Down Expand Up @@ -93,6 +93,7 @@ lazy val leaderboard = project
},
scalacOptions -= "-Xfatal-warnings",
scalacOptions -= "-Ykind-projector",
scalacOptions -= "-Wnonunit-statement",
scalacOptions ++= {
if (scalaVersion.value.startsWith("2")) {
Seq(
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/leaderboard/api/ProfileApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ProfileApi[F[+_, +_]: Async2: Fork2: Primitives2](
case rq @ POST -> Root / "profile" / UUIDVar(userId) =>
Ok(for {
profile <- rq.decodeJson[UserProfile]
_ <- log.info(s"Sending $profile")
_ <- log.info(s"Saving $profile")
_ <- profiles.setProfile(userId, profile)
} yield ())
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/scala/leaderboard/http/HttpServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ package leaderboard.http

import cats.effect.Async
import cats.implicits.*
import distage.Id
import com.comcast.ip4s.Port
import fs2.io.net.Network
import izumi.distage.model.definition.Lifecycle
import leaderboard.api.HttpApi
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.ember.server.EmberServerBuilder
import org.http4s.server.Server

import scala.concurrent.ExecutionContext

final case class HttpServer(
server: Server
)

object HttpServer {

final class Impl[F[+_, +_]](
allHttpApis: Set[HttpApi[F]],
cpuPool: ExecutionContext @Id("cpu"),
allHttpApis: Set[HttpApi[F]]
)(implicit
async: Async[F[Throwable, _]]
) extends Lifecycle.Of[F[Throwable, _], HttpServer](
Lifecycle.fromCats {
val combinedApis = allHttpApis.map(_.http).toList.foldK

BlazeServerBuilder[F[Throwable, _]]
.withExecutionContext(cpuPool)
EmberServerBuilder
.default(async, Network.forAsync)
.withHttpApp(combinedApis.orNotFound)
.bindLocal(8080)
.resource
.withPort(Port.fromInt(8080).get)
.build
.map(HttpServer(_))
}
)
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/test/scala/leaderboard/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import izumi.distage.model.definition.Activation
import izumi.distage.model.definition.StandardAxis.Repo
import izumi.distage.plugins.PluginConfig
import izumi.distage.testkit.scalatest.{AssertIO3, Spec3}
import leaderboard.model.{QueryFailure, RankedProfile, Score, UserId, UserProfile}
import leaderboard.model.*
import leaderboard.repo.{Ladder, Profiles}
import leaderboard.services.Ranks
import leaderboard.zioenv.*
Expand Down Expand Up @@ -86,7 +86,7 @@ abstract class LadderTest extends LeaderboardTest {
assertIO(user1Rank < user2Rank)
} else if (score2 > score1) {
assertIO(user2Rank < user1Rank)
} else IO.unit
} else ZIO.unit
} yield ()
}

Expand All @@ -100,7 +100,7 @@ abstract class ProfilesTest extends LeaderboardTest {

/** that's what the ZIO signature looks like for ZIO Env injection: */
"set & get" in {
val zioValue: ZIO[ProfilesEnv & RndEnv, QueryFailure, Unit] = for {
val zioValue: ZIO[Profiles[IO] & Rnd[IO], QueryFailure, Unit] = for {
user <- rnd[UserId]
name <- rnd[String]
desc <- rnd[String]
Expand Down Expand Up @@ -170,7 +170,7 @@ abstract class RanksTest extends LeaderboardTest {
assertIO(user1Rank < user2Rank)
} else if (score2 > score1) {
assertIO(user2Rank < user1Rank)
} else IO.unit
} else ZIO.unit
} yield ()
}

Expand Down
27 changes: 11 additions & 16 deletions src/test/scala/leaderboard/zioenv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,26 @@ import leaderboard.model.*
import leaderboard.repo.{Ladder, Profiles}
import leaderboard.services.Ranks
import org.scalacheck.Arbitrary
import zio.{Has, IO, URIO, ZIO}
import zio.{IO, URIO, ZIO}

object zioenv {

object ladder extends Ladder[ZIO[LadderEnv, _, _]] {
def submitScore(userId: UserId, score: Score): ZIO[LadderEnv, QueryFailure, Unit] = ZIO.accessM(_.get.submitScore(userId, score))
def getScores: ZIO[LadderEnv, QueryFailure, List[(UserId, Score)]] = ZIO.accessM(_.get.getScores)
object ladder extends Ladder[ZIO[Ladder[IO], _, _]] {
def submitScore(userId: UserId, score: Score): ZIO[Ladder[IO], QueryFailure, Unit] = ZIO.serviceWithZIO(_.submitScore(userId, score))
def getScores: ZIO[Ladder[IO], QueryFailure, List[(UserId, Score)]] = ZIO.serviceWithZIO(_.getScores)
}

object profiles extends Profiles[ZIO[ProfilesEnv, _, _]] {
override def setProfile(userId: UserId, profile: UserProfile): ZIO[ProfilesEnv, QueryFailure, Unit] = ZIO.accessM(_.get.setProfile(userId, profile))
override def getProfile(userId: UserId): ZIO[ProfilesEnv, QueryFailure, Option[UserProfile]] = ZIO.accessM(_.get.getProfile(userId))
object profiles extends Profiles[ZIO[Profiles[IO], _, _]] {
override def setProfile(userId: UserId, profile: UserProfile): ZIO[Profiles[IO], QueryFailure, Unit] = ZIO.serviceWithZIO(_.setProfile(userId, profile))
override def getProfile(userId: UserId): ZIO[Profiles[IO], QueryFailure, Option[UserProfile]] = ZIO.serviceWithZIO(_.getProfile(userId))
}

object ranks extends Ranks[ZIO[RanksEnv, _, _]] {
override def getRank(userId: UserId): ZIO[RanksEnv, QueryFailure, Option[RankedProfile]] = ZIO.accessM(_.get.getRank(userId))
object ranks extends Ranks[ZIO[Ranks[IO], _, _]] {
override def getRank(userId: UserId): ZIO[Ranks[IO], QueryFailure, Option[RankedProfile]] = ZIO.serviceWithZIO(_.getRank(userId))
}

object rnd extends Rnd[ZIO[RndEnv, _, _]] {
override def apply[A: Arbitrary]: URIO[RndEnv, A] = ZIO.accessM(_.get.apply[A])
object rnd extends Rnd[ZIO[Rnd[IO], _, _]] {
override def apply[A: Arbitrary]: URIO[Rnd[IO], A] = ZIO.serviceWithZIO(_.apply[A])
}

type LadderEnv = Has[Ladder[IO]]
type ProfilesEnv = Has[Profiles[IO]]
type RanksEnv = Has[Ranks[IO]]
type RndEnv = Has[Rnd[IO]]

}

0 comments on commit 1ecc3d0

Please sign in to comment.