Skip to content

Commit

Permalink
update zio-http
Browse files Browse the repository at this point in the history
  • Loading branch information
kitlangton committed Nov 3, 2022
1 parent 0365c53 commit 339a3e5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 84 deletions.
52 changes: 26 additions & 26 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ inThisBuild(
"kitlangton",
"Kit Langton",
"[email protected]",
url("https://github.com/kitlangton"),
),
url("https://github.com/kitlangton")
)
),
sonatypeCredentialHost := "s01.oss.sonatype.org",
),
sonatypeCredentialHost := "s01.oss.sonatype.org"
)
)

lazy val supportedScalaVersions = List(scala213)
Expand All @@ -38,32 +38,32 @@ val postgresVersion = "42.3.6"
val quillVersion = "4.1.0"
val scalaJavaTimeVersion = "2.4.0"
val sttpVersion = "3.7.1"
val zioHttpVersion = "2.0.0-RC10"
val zioHttpVersion = "2.0.0-RC11"
val zioJsonVersion = "0.3.0-RC3"
val zioNioVersion = "2.0.0"
val zioProcessVersion = "0.7.1"
val zioVersion = "2.0.0"
val zioVersion = "2.0.3"
val zioQueryVersion = "0.3.0"

val sharedSettings = Seq(
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.2" cross CrossVersion.full),
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1"),
resolvers ++= Seq(
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"Sonatype OSS Snapshots s01" at "https://s01.oss.sonatype.org/content/repositories/snapshots",
"Sonatype OSS Snapshots s01" at "https://s01.oss.sonatype.org/content/repositories/snapshots"
),
libraryDependencies ++= Seq(
"io.suzaku" %%% "boopickle" % boopickleVerison,
"dev.zio" %%% "zio" % zioVersion,
"dev.zio" %%% "zio-streams" % zioVersion,
"dev.zio" %%% "zio-test" % zioVersion % Test,
"com.lihaoyi" %%% "fansi" % fansiVersion,
"com.lihaoyi" %%% "fansi" % fansiVersion
),
scalacOptions ++= Seq("-Ymacro-annotations", "-Xfatal-warnings", "-deprecation"),
scalaVersion := scala213,
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
// semanticdbVersion := scalafixSemanticdb.revision, // only required for Scala 2.x,
scalacOptions += "-Yrangepos",
scalacOptions += "-Yrangepos"
)

lazy val root = (project in file("."))
Expand All @@ -73,7 +73,7 @@ lazy val root = (project in file("."))
// crossScalaVersions must be set to Nil on the aggregating project
crossScalaVersions := Nil,
publish / skip := true,
welcomeMessage,
welcomeMessage
)

lazy val cli = (project in file("cli"))
Expand Down Expand Up @@ -108,21 +108,21 @@ lazy val cli = (project in file("cli"))
"--initialize-at-run-time=io.netty.util.AbstractReferenceCounted",
"--initialize-at-run-time=io.netty.channel.kqueue.KQueue",
"--initialize-at-build-time=org.slf4j.LoggerFactory",
"-H:IncludeResources='.*'",
"-H:IncludeResources='.*'"
),
libraryDependencies ++= Seq(
"dev.zio" %% "zio-process" % zioProcessVersion,
"dev.zio" %% "zio-nio" % zioNioVersion,
"dev.zio" %% "zio-parser" % "0.1.7",
"io.d11" %% "zhttp" % zioHttpVersion,
"org.jline" % "jline" % "3.21.0",
"org.jline" % "jline" % "3.21.0"
),
resolvers ++= Seq(
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"Sonatype OSS Snapshots s01" at "https://s01.oss.sonatype.org/content/repositories/snapshots",
"Sonatype OSS Snapshots s01" at "https://s01.oss.sonatype.org/content/repositories/snapshots"
),
Compile / mainClass := Some("zio.app.Main"),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
)
.settings(sharedSettings)
.dependsOn(cliShared, coreJVM)
Expand All @@ -146,8 +146,8 @@ lazy val cliFrontend = project
"io.github.cquiroz" %%% "scala-java-time-tzdb" % scalaJavaTimeVersion,
"io.laminext" %%% "websocket" % laminextVersion,
"com.softwaremill.sttp.client3" %%% "core" % sttpVersion,
"com.softwaremill.sttp.client3" %%% "monix" % sttpVersion,
),
"com.softwaremill.sttp.client3" %%% "monix" % sttpVersion
)
)
.settings(sharedSettings)
.dependsOn(cliShared, coreJS)
Expand All @@ -163,7 +163,7 @@ lazy val cliShared = project
},
scalaJSLinkerConfig ~= {
_.withSourceMap(false)
},
}
)

lazy val core = crossProject(JSPlatform, JVMPlatform)
Expand All @@ -179,7 +179,7 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
resolvers ++= Seq(
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"Sonatype OSS Snapshots s01" at "https://s01.oss.sonatype.org/content/repositories/snapshots",
"Sonatype OSS Snapshots s01" at "https://s01.oss.sonatype.org/content/repositories/snapshots"
),
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
Expand All @@ -192,8 +192,8 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
"com.softwaremill.sttp.client3" %%% "core" % sttpVersion,
"io.getquill" %% "quill-jdbc-zio" % quillVersion,
"org.postgresql" % "postgresql" % postgresVersion,
"org.scalameta" %% "scalameta" % "4.5.9",
),
"org.scalameta" %% "scalameta" % "4.5.9"
)
)

lazy val coreJS = core.js
Expand All @@ -208,14 +208,14 @@ lazy val examples = crossProject(JSPlatform, JVMPlatform)
libraryDependencies ++= Seq(
"dev.zio" %%% "zio" % zioVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"io.d11" %% "zhttp" % zioHttpVersion,
),
"io.d11" %% "zhttp" % zioHttpVersion
)
)
.jvmSettings(
libraryDependencies ++= Seq(
"com.softwaremill.sttp.client3" %%% "core" % sttpVersion,
"com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % sttpVersion,
),
"com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % sttpVersion
)
)
.jsSettings(
scalaJSLinkerConfig ~= {
Expand All @@ -228,8 +228,8 @@ lazy val examples = crossProject(JSPlatform, JVMPlatform)
libraryDependencies ++= Seq(
"com.raquo" %%% "laminar" % laminarVersion,
"io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion,
"io.github.cquiroz" %%% "scala-java-time-tzdb" % scalaJavaTimeVersion,
),
"io.github.cquiroz" %%% "scala-java-time-tzdb" % scalaJavaTimeVersion
)
)
.dependsOn(core)

Expand Down
64 changes: 32 additions & 32 deletions cli/src/main/scala/tui/TerminalApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ sealed trait TerminalEvent[+I]

trait TUI {
def run[I, S, A](
terminalApp: TerminalApp[I, S, A],
events: ZStream[Any, Throwable, I],
initialState: S
terminalApp: TerminalApp[I, S, A],
events: ZStream[Any, Throwable, I],
initialState: S
): Task[Option[A]]
}

Expand All @@ -52,7 +52,7 @@ object TUI {
ZIO.serviceWithZIO[TUI](_.run(terminalApp, ZStream.never, initialState))

def runWithEvents[I, S, A](
terminalApp: TerminalApp[I, S, A]
terminalApp: TerminalApp[I, S, A]
)(events: ZStream[Any, Throwable, I], initialState: S): RIO[TUI, Option[A]] =
ZIO.serviceWithZIO[TUI](_.run(terminalApp, events, initialState))
}
Expand All @@ -61,9 +61,9 @@ case class TUILive(fullScreen: Boolean) extends TUI {
var lastSize: Size = Size(0, 0)

def run[I, S, A](
terminalApp: TerminalApp[I, S, A],
events: ZStream[Any, Throwable, I],
initialState: S
terminalApp: TerminalApp[I, S, A],
events: ZStream[Any, Throwable, I],
initialState: S
): Task[Option[A]] =
ZIO.scoped {
Input
Expand All @@ -75,35 +75,35 @@ case class TUILive(fullScreen: Boolean) extends TUI {
oldMap: Ref[TextMap] <- Ref.make(TextMap.ofDim(0, 0))

_ <- (for {
_ <- ZIO.succeed(Input.ec.clear())
(width, height) <- ZIO.succeed(Input.terminalSize)
_ <- renderFullScreen(oldMap, terminalApp, initialState, width, height)
} yield ()).when(fullScreen)
_ <- ZIO.succeed(Input.ec.clear())
(width, height) <- ZIO.succeed(Input.terminalSize)
_ <- renderFullScreen(oldMap, terminalApp, initialState, width, height)
} yield ()).when(fullScreen)

renderStream =
stateRef.changes
.zipWithLatest(Input.terminalSizeStream)((_, _))
.zipLatestWith(Input.terminalSizeStream)((_, _))
.tap { case (state, (width, height)) =>
if (fullScreen) renderFullScreen(oldMap, terminalApp, state, width, height)
else renderTerminal(terminalApp, state)
}

updateStream = Input.keyEventStream.mergeEither(events).tap { keyEvent =>
val event = keyEvent match {
case Left(value) => TerminalEvent.SystemEvent(value)
case Right(value) => TerminalEvent.UserEvent(value)
}

stateRef.updateZIO { state =>
terminalApp.update(state, event) match {
case Step.Update(state) => ZIO.succeed(state)
case Step.Done(result) => resultPromise.succeed(Some(result)).as(state)
case Step.Exit => resultPromise.succeed(None).as(state)
}
}
}

_ <- ZStream.mergeAllUnbounded()(renderStream, updateStream).interruptWhen(resultPromise.await).runDrain
val event = keyEvent match {
case Left(value) => TerminalEvent.SystemEvent(value)
case Right(value) => TerminalEvent.UserEvent(value)
}

stateRef.updateZIO { state =>
terminalApp.update(state, event) match {
case Step.Update(state) => ZIO.succeed(state)
case Step.Done(result) => resultPromise.succeed(Some(result)).as(state)
case Step.Exit => resultPromise.succeed(None).as(state)
}
}
}

_ <- ZStream.mergeAllUnbounded()(renderStream, updateStream).interruptWhen(resultPromise.await).runDrain
result <- resultPromise.await
} yield result
}
Expand All @@ -113,11 +113,11 @@ case class TUILive(fullScreen: Boolean) extends TUI {
var lastWidth = 0

def renderFullScreen[I, S, A](
oldMap: Ref[TextMap],
terminalApp: TerminalApp[I, S, A],
state: S,
width: Int,
height: Int
oldMap: Ref[TextMap],
terminalApp: TerminalApp[I, S, A],
state: S,
width: Int,
height: Int
): UIO[Unit] =
oldMap.update { oldMap =>
if (lastWidth != width || lastHeight != height) {
Expand Down
24 changes: 12 additions & 12 deletions cli/src/main/scala/zio/app/DevMode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ object DevMode {
.unwrap(
for {
process <- Command("sbt", command, "--color=always").run
.tap(_.exitCode.fork)
.tap(_.exitCode.fork)
errorStream = ZStream
.fromZIO(process.stderr.lines.flatMap { lines =>
val errorString = lines.mkString
if (errorString.contains("waiting for lock"))
ZIO.fail(SbtError.WaitingForLock)
else if (errorString.contains("Invalid commands"))
ZIO.fail(SbtError.InvalidCommand(s"sbt $command"))
else {
println(s"ERRRRRRR ${errorString}")
ZIO.fail(SbtError.SbtErrorMessage(errorString))
}
})
.fromZIO(process.stderr.lines.flatMap { lines =>
val errorString = lines.mkString
if (errorString.contains("waiting for lock"))
ZIO.fail(SbtError.WaitingForLock)
else if (errorString.contains("Invalid commands"))
ZIO.fail(SbtError.InvalidCommand(s"sbt $command"))
else {
println(s"ERRRRRRR ${errorString}")
ZIO.fail(SbtError.SbtErrorMessage(errorString))
}
})
} yield ZStream.mergeAllUnbounded()(
ZStream.succeed(s"sbt $command"),
process.stdout.linesStream,
Expand Down
26 changes: 13 additions & 13 deletions core/shared/src/main/scala/zio/app/internal/BackendUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ object BackendUtils {
def makeRoute[R, E: Pickler, A: Pickler, B: Pickler](
service: String,
method: String,
call: A => ZIO[R, E, B],
call: A => ZIO[R, E, B]
): HttpApp[R, Throwable] = {
val service0 = urlEncode(service)
val method0 = method
Http.collectZIO { case post @ Method.POST -> !! / `service0` / `method0` =>
post.body.orDie.flatMap { body =>
val byteBuffer = ByteBuffer.wrap(body.toArray)
post.body.asArray.orDie.flatMap { body =>
val byteBuffer = ByteBuffer.wrap(body)
val unpickled = Unpickle[A].fromBytes(byteBuffer)
call(unpickled)
.map(pickle[B](_))
Expand All @@ -45,7 +45,7 @@ object BackendUtils {
def makeRouteNullary[R, E: Pickler, A: Pickler](
service: String,
method: String,
call: ZIO[R, E, A],
call: ZIO[R, E, A]
): HttpApp[R, Throwable] = {
val service0 = urlEncode(service)
val method0 = method
Expand All @@ -64,13 +64,13 @@ object BackendUtils {
def makeRouteStream[R, E: Pickler, A: Pickler, B: Pickler](
service: String,
method: String,
call: A => ZStream[R, E, B],
call: A => ZStream[R, E, B]
): HttpApp[R, Nothing] = {
val service0 = service
val method0 = method
Http.collectZIO { case post @ Method.POST -> !! / `service0` / `method0` =>
post.body.orDie.flatMap { body =>
val byteBuffer = ByteBuffer.wrap(body.toArray)
post.body.asArray.orDie.flatMap { body =>
val byteBuffer = ByteBuffer.wrap(body)
val unpickled = Unpickle[A].fromBytes(byteBuffer)
ZIO.environment[R].map { env =>
makeStreamResponse(call(unpickled), env)
Expand All @@ -82,7 +82,7 @@ object BackendUtils {
def makeRouteNullaryStream[R, E: Pickler, A: Pickler](
service: String,
method: String,
call: ZStream[R, E, A],
call: ZStream[R, E, A]
): HttpApp[R, Nothing] = {
val service0 = service
val method0 = method
Expand All @@ -96,14 +96,14 @@ object BackendUtils {
private def pickle[A: Pickler](value: A): Response = {
val bytes: ByteBuffer = Pickle.intoBytes(value)
val byteBuf = Unpooled.wrappedBuffer(bytes)
val httpData = HttpData.fromByteBuf(byteBuf)
val body = Body.fromByteBuf(byteBuf)

Response(status = Status.Ok, headers = Headers(bytesContent), data = httpData)
Response(status = Status.Ok, headers = Headers(bytesContent), body = body)
}

private def makeStreamResponse[A: Pickler, E: Pickler, R](
stream: ZStream[R, E, A],
env: ZEnvironment[R],
env: ZEnvironment[R]
): Response = {
val responseStream: ZStream[Any, Throwable, Byte] =
stream.mapConcatChunk { a =>
Expand All @@ -115,7 +115,7 @@ object BackendUtils {
}
.provideEnvironment(env)

Response(data = HttpData.fromStream(responseStream))
Response(body = Body.fromStream(responseStream))
}

}
Expand All @@ -132,7 +132,7 @@ object CustomPicklers {
// local date time
implicit val localDateTimePickler: Pickler[java.time.LocalDateTime] =
transformPickler((t: Long) =>
java.time.LocalDateTime.ofInstant(Instant.ofEpochMilli(t), java.time.ZoneId.of("UTC")),
java.time.LocalDateTime.ofInstant(Instant.ofEpochMilli(t), java.time.ZoneId.of("UTC"))
)(_.toInstant(java.time.ZoneOffset.UTC).toEpochMilli)

}
2 changes: 1 addition & 1 deletion examples/jvm/src/main/scala/examples/Backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ object Backend extends ZIOAppDefault {
} yield ())
.provide(
ExampleServiceLive.layer,
ParameterizedServiceLive.layer
ParameterizedServiceLive.layer,
)
}

0 comments on commit 339a3e5

Please sign in to comment.