-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of zio-http web-sockets support
- Loading branch information
1 parent
40614d7
commit 97444d8
Showing
7 changed files
with
176 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
server/zio-http-server/src/main/scala/sttp/tapir/server/ziohttp/ZioWebSockets.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package sttp.tapir.server.ziohttp | ||
import sttp.capabilities.zio.ZioStreams | ||
import sttp.capabilities.zio.ZioStreams.Pipe | ||
import sttp.tapir.model.WebSocketFrameDecodeFailure | ||
import sttp.tapir.{DecodeResult, WebSocketBodyOutput} | ||
import sttp.ws.WebSocketFrame | ||
import zio.http.{WebSocketFrame => ZWebSocketFrame} | ||
import zio.stream.ZStream | ||
import zio.{Chunk, ZIO} | ||
|
||
object ZioWebSockets { | ||
def pipeToBody[REQ, RESP]( | ||
pipe: Pipe[REQ, RESP], | ||
o: WebSocketBodyOutput[Pipe[REQ, RESP], REQ, RESP, _, ZioStreams] | ||
): F2F = { in => | ||
ZStream | ||
.from(in) | ||
.map(zFrameToFrame) | ||
.map { | ||
case WebSocketFrame.Close(_, _) if !o.decodeCloseRequests => None | ||
case f => | ||
o.requests.decode(f) match { | ||
case failure: DecodeResult.Failure => throw new WebSocketFrameDecodeFailure(f, failure) | ||
case DecodeResult.Value(v) => Some(v) | ||
} | ||
} | ||
.collectWhileSome | ||
.viaFunction(pipe) | ||
.map(o.responses.encode) | ||
.map(frameToZFrame) | ||
.tap(v => zio.ZIO.succeed(println(v))) | ||
.runFoldZIO(List.empty[ZWebSocketFrame])((s, ws) => ZIO.succeed(ws +: s)) | ||
} | ||
|
||
private def zFrameToFrame(f: ZWebSocketFrame): WebSocketFrame = | ||
f match { | ||
case ZWebSocketFrame.Text(text) => WebSocketFrame.Text(text, f.isFinal, rsv = None) | ||
case ZWebSocketFrame.Binary(buffer) => WebSocketFrame.Binary(buffer.toArray, f.isFinal, rsv = None) | ||
case ZWebSocketFrame.Continuation(buffer) => WebSocketFrame.Binary(buffer.toArray, f.isFinal, rsv = None) | ||
case ZWebSocketFrame.Ping => WebSocketFrame.ping | ||
case ZWebSocketFrame.Pong => WebSocketFrame.pong | ||
case ZWebSocketFrame.Close(status, reason) => WebSocketFrame.Close(status, reason.getOrElse("")) | ||
case _ => WebSocketFrame.Binary(Array.empty[Byte], f.isFinal, rsv = None) | ||
} | ||
|
||
private def frameToZFrame(f: WebSocketFrame): ZWebSocketFrame = | ||
f match { | ||
case WebSocketFrame.Text(p, finalFragment, _) => ZWebSocketFrame.Text(p, finalFragment) | ||
case WebSocketFrame.Binary(p, finalFragment, _) => ZWebSocketFrame.Binary(Chunk.fromArray(p), finalFragment) | ||
case WebSocketFrame.Ping(_) => ZWebSocketFrame.Ping | ||
case WebSocketFrame.Pong(_) => ZWebSocketFrame.Pong | ||
case WebSocketFrame.Close(code, reason) => ZWebSocketFrame.Close(code, Some(reason)) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
server/zio-http-server/src/main/scala/sttp/tapir/server/ziohttp/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package sttp.tapir.server | ||
import zio.Task | ||
import zio.http.{WebSocketFrame => ZWebSocketFrame} | ||
|
||
package object ziohttp { | ||
type F2F = ZWebSocketFrame => Task[List[ZWebSocketFrame]] | ||
|
||
type ZioResponseBody = | ||
Either[F2F, ZioHttpResponseBody] | ||
|
||
} |
Oops, something went wrong.