-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1b14fd
commit 1cf911d
Showing
22 changed files
with
820 additions
and
189 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
29 changes: 29 additions & 0 deletions
29
examples/src/main/scala/sttp/tapir/examples/HelloWorldNettySyncServer.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,29 @@ | ||
package sttp.tapir.examples | ||
|
||
import ox.* | ||
import sttp.tapir.* | ||
import sttp.tapir.server.netty.loom.{Id, NettySyncServer} | ||
|
||
object HelloWorldNettySyncServer: | ||
val helloWorld = endpoint.get | ||
.in("hello") | ||
.in(query[String]("name")) | ||
.out(stringBody) | ||
.serverLogicSuccess[Id](name => s"Hello, $name!") | ||
|
||
NettySyncServer().addEndpoint(helloWorld).startAndWait() | ||
|
||
// Alternatively, if you need manual control of the structured concurrency scope, server lifecycle, | ||
// or just metadata from `NettySyncServerBinding` (like port number), use `start()`: | ||
object HelloWorldNettySyncServer2: | ||
val helloWorld = endpoint.get | ||
.in("hello") | ||
.in(query[String]("name")) | ||
.out(stringBody) | ||
.serverLogicSuccess[Id](name => s"Hello, $name!") | ||
|
||
supervised { | ||
val serverBinding = useInScope(NettySyncServer().addEndpoint(helloWorld).start())(_.stop()) | ||
println(s"Tapir is running on port ${serverBinding.port}") | ||
never | ||
} |
56 changes: 56 additions & 0 deletions
56
examples/src/main/scala/sttp/tapir/examples/websocket/WebSocketNettySyncServer.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,56 @@ | ||
package sttp.tapir.examples.websocket | ||
|
||
import ox.* | ||
import ox.channels.* | ||
import sttp.capabilities.WebSockets | ||
import sttp.tapir.* | ||
import sttp.tapir.server.netty.loom.Id | ||
import sttp.tapir.server.netty.loom.OxStreams | ||
import sttp.tapir.server.netty.loom.OxStreams.Pipe // alias for Ox ?=> Source[A] => Source[B] | ||
import sttp.tapir.server.netty.loom.NettySyncServer | ||
import sttp.ws.WebSocketFrame | ||
|
||
import scala.concurrent.duration.* | ||
|
||
object WebSocketNettySyncServer: | ||
// Web socket endpoint | ||
val wsEndpoint = | ||
endpoint.get | ||
.in("ws") | ||
.out( | ||
webSocketBody[String, CodecFormat.TextPlain, String, CodecFormat.TextPlain](OxStreams) | ||
.concatenateFragmentedFrames(false) // All these options are supported by tapir-netty | ||
.ignorePong(true) | ||
.autoPongOnPing(true) | ||
.decodeCloseRequests(false) | ||
.decodeCloseResponses(false) | ||
.autoPing(Some((10.seconds, WebSocketFrame.Ping("ping-content".getBytes)))) | ||
) | ||
|
||
// Your processor transforming a stream of requests into a stream of responses | ||
val wsPipe: Pipe[String, String] = requestStream => requestStream.map(_.toUpperCase) | ||
// Alternatively, requests and responses can be treated separately, for example to emit frames to the client from another source: | ||
val wsPipe2: Pipe[String, String] = { in => | ||
fork { | ||
in.drain() // read and ignore requests | ||
} | ||
// emit periodic responses | ||
Source.tick(1.second).map(_ => System.currentTimeMillis()).map(_.toString) | ||
} | ||
|
||
// The WebSocket endpoint, builds the pipeline in serverLogicSuccess | ||
val wsServerEndpoint = wsEndpoint.serverLogicSuccess[Id](_ => wsPipe) | ||
|
||
// A regular /GET endpoint | ||
val helloWorldEndpoint = | ||
endpoint.get.in("hello").in(query[String]("name")).out(stringBody) | ||
|
||
val helloWorldServerEndpoint = helloWorldEndpoint | ||
.serverLogicSuccess[Id](name => s"Hello, $name!") | ||
|
||
def main(args: Array[String]): Unit = | ||
NettySyncServer() | ||
.host("0.0.0.0") | ||
.port(8080) | ||
.addEndpoints(List(wsServerEndpoint, helloWorldServerEndpoint)) | ||
.startAndWait() |
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
32 changes: 0 additions & 32 deletions
32
perf-tests/src/main/scala/sttp/tapir/perf/netty/loom/NettySync.scala
This file was deleted.
Oops, something went wrong.
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
11 changes: 11 additions & 0 deletions
11
server/netty-server/loom/src/main/scala/sttp/tapir/server/netty/loom/NettyOxStreams.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.netty.loom | ||
|
||
import ox.Ox | ||
import ox.channels.Source | ||
import sttp.capabilities.Streams | ||
|
||
trait OxStreams extends Streams[OxStreams]: | ||
override type BinaryStream = Nothing | ||
override type Pipe[A, B] = Ox ?=> Source[A] => Source[B] | ||
|
||
object OxStreams extends OxStreams |
Oops, something went wrong.