Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:18
Show Gist options
  • Save dacr/69caa9bf4283f799e0768cdf1d6bd088 to your computer and use it in GitHub Desktop.
Save dacr/69caa9bf4283f799e0768cdf1d6bd088 to your computer and use it in GitHub Desktop.
ZIO learning - zhttp - streaming from a java stream / published by https://github.com/dacr/code-examples-manager #de9fa625-5d52-4e22-99de-bb847c7a8353/9ce37c780f5751535739ca73ec4df03b653ba94e
// summary : ZIO learning - zhttp - streaming from a java stream
// keywords : scala, zio, learning, pure-functional, http-server, zhttp, streams, @testable, @exclusive, @fail
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : de9fa625-5d52-4e22-99de-bb847c7a8353
// created-on : 2024-01-05T18:44:19+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// test-with : curl http://127.0.0.1:8080/
// ---------------------
//> using scala "3.4.2"
//> using dep "dev.zio::zio-http:3.0.0-RC4"
// ---------------------
import zio.*, zio.http.*, zio.stream.*
import java.util.stream.{Stream => JStream}
// SEE https://github.com/zio/zio-http/issues/2584
object Main extends ZIOAppDefault {
def getJavaStream: Task[JStream[String]] = ZIO.attempt {
throw RuntimeException("failure")
//JStream.of("1", "2", "3", "4")
}
override def run =
for {
_ <- ZIO.logInfo("started")
stream = ZStream.fromJavaStreamZIO(getJavaStream).flatMap(s => ZStream.fromIterable(s.getBytes))
app = Routes(Method.GET / "stream" -> handler(Response(body = Body.fromStream(stream)))).toHttpApp
_ <- Server.serve(app).provide(Server.default)
} yield ()
}
Main.main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment