Skip to content

Commit

Permalink
doc: update StreamExample
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Apr 19, 2021
1 parent 9be242c commit 04220a7
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions example/src/main/scala/StreamingResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@ import zio._
import zio.stream.ZStream

/**
* In this example we send Response content as, a Stream of Chunk[Byte]
* Example to encode content using a ZStream
*/
object StreamingResponse extends App {
// message as Chunk[Byte]
// Create a message as a Chunk[Byte]
val message = Chunk.fromArray("Hello world !\r\n".getBytes(HTTP_CHARSET))
val app = Http.collect {

// Use `Http.collect` to match on route
val app = Http.collect {

// Simple (non-stream) based route
case Method.GET -> Root / "health" => Response.ok

// ZStream powered response
case Method.GET -> Root / "stream" =>
Response.http(
status = Status.OK,
headers = List(Header.contentLength(message.length.toLong)),
content = HttpData.fromStream(ZStream.succeed(message)), // creates Content as Stream
content = HttpData.fromStream(ZStream.succeed(message)), // Encoding content using a ZStream
)

}
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {

/**
* To configure the server refer example [[HelloWorldAdvanced]]
*/
// Starting the server (for more advanced startup configuration checkout `HelloWorldAdvanced`)
Server.start(8090, app.silent).exitCode
}
}

0 comments on commit 04220a7

Please sign in to comment.