Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 08:38
Show Gist options
  • Save dacr/2b81c1ce8f6c6bc31aadf866064524d7 to your computer and use it in GitHub Desktop.
Save dacr/2b81c1ce8f6c6bc31aadf866064524d7 to your computer and use it in GitHub Desktop.
Streamed hello world / published by https://github.com/dacr/code-examples-manager #ee6c3275-18d1-4651-a6fa-7b27371d09b2/72869f114f00b13635b288971d2236a23db898c5
// summary : Streamed hello world
// keywords : scala, zio, tapir, http, zhttp, stream, @testable, @exclusive
// 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 : ee6c3275-18d1-4651-a6fa-7b27371d09b2
// created-on : 2024-05-15T09:53:10+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// test-with : curl http://127.0.0.1:8080/hello
// ---------------------
//> using scala "3.4.2"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio:1.10.7"
//> using dep "com.softwaremill.sttp.tapir::tapir-zio-http-server:1.10.7"
//> using dep "com.softwaremill.sttp.tapir::tapir-json-zio:1.10.7"
// ---------------------
import sttp.tapir.ztapir.*
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import zio.*
import zio.stream.*
import zio.json.*
import zio.http.Server
import sttp.capabilities.zio.ZioStreams
import sttp.model.{MediaType, StatusCode}
import sttp.tapir.{CodecFormat, Schema}
import sttp.tapir.generic.auto.*
import sttp.tapir.json.zio.*
import sttp.tapir.ztapir.*
case class Greeting(message:String) derives JsonCodec
case class JsonSeqCodecFormat() extends CodecFormat {
override val mediaType: MediaType = MediaType.unsafeApply("application", "json-seq")
}
object WebApp extends ZIOAppDefault {
// --------------------------------------------------
val greetingStream: ZIO[Any, String, ZStream[Any, Throwable, Byte]] = ZIO.succeed(
ZStream
.repeat(Greeting("Hello world"))
.schedule(Schedule.spaced(1.second))
.flatMap(greeting => ZStream.fromIterable( (greeting.toJson+"\n").getBytes ))
)
val failingGreetingStream1: ZIO[Any, String, ZStream[Any, Throwable, Byte]] = ZIO.fail(
"Can not build the stream"
)
val failingGreetingStream2: ZIO[Any, String, ZStream[Any, Throwable, Byte]] = ZIO.succeed(
ZStream.fail(Exception("Can not build the stream"))
) // of course http success here
val failingGreetingStream3: ZIO[Any, String, ZStream[Any, Throwable, Byte]] = ZIO.succeed(
ZStream.fromJavaStreamZIO(
ZIO.fail(Exception("Can not build the stream"))
)
) // of course http success here
val helloEndPoint =
endpoint
.description("Returns greeting")
.get
.in("hello")
.out(streamBody(ZioStreams)(Schema.derived[Greeting], JsonSeqCodecFormat()))
.out(statusCode(StatusCode.Ok).description("query success"))
.errorOutVariantPrepend(oneOfVariant(StatusCode.InternalServerError, plainBody[String]))
//val helloRoute = helloEndPoint.zServerLogic[Any](_ => greetingStream)
val helloRoute = helloEndPoint.zServerLogic[Any](_ => failingGreetingStream3)
val routes = ZioHttpInterpreter().toHttp(List(helloRoute))
override def run = Server.serve(routes).provide(Server.default)
}
WebApp.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment