-
Notifications
You must be signed in to change notification settings - Fork 411
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
Showing
16 changed files
with
158 additions
and
35 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,32 @@ | ||
# Concrete Enity | ||
```scala | ||
import zhttp.http._ | ||
import zhttp.service.Server | ||
import zio._ | ||
|
||
/** | ||
* Example to build app on concrete entity | ||
*/ | ||
object ConcreteEntity extends ZIOAppDefault { | ||
// Request | ||
case class CreateUser(name: String) | ||
|
||
// Response | ||
case class UserCreated(id: Long) | ||
|
||
val user: Http[Any, Nothing, CreateUser, UserCreated] = | ||
Http.collect[CreateUser] { case CreateUser(_) => | ||
UserCreated(2) | ||
} | ||
|
||
val app: HttpApp[Any, Nothing] = | ||
user | ||
.contramap[Request](req => CreateUser(req.path.toString)) // Http[Any, Nothing, Request, UserCreated] | ||
.map(userCreated => Response.text(userCreated.id.toString)) // Http[Any, Nothing, Request, Response] | ||
|
||
// Run it like any simple app | ||
val run = | ||
Server.start(8090, app) | ||
} | ||
|
||
``` |
Empty file.
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,34 @@ | ||
# Streaming Response | ||
|
||
```scala | ||
import zhttp.http._ | ||
import zhttp.service.Server | ||
import zio.stream.ZStream | ||
import zio._ | ||
|
||
/** | ||
* Example to encode content using a ZStream | ||
*/ | ||
object StreamingResponse extends ZIOAppDefault { | ||
// Starting the server (for more advanced startup configuration checkout `HelloWorldAdvanced`) | ||
override def run = Server.start(8090, app.silent) | ||
|
||
// Create a message as a Chunk[Byte] | ||
val message = Chunk.fromArray("Hello world !\r\n".getBytes(HTTP_CHARSET)) | ||
// Use `Http.collect` to match on route | ||
val app: HttpApp[Any, Nothing] = Http.collect[Request] { | ||
|
||
// Simple (non-stream) based route | ||
case Method.GET -> !! / "health" => Response.ok | ||
|
||
// ZStream powered response | ||
case Method.GET -> !! / "stream" => | ||
Response( | ||
status = Status.OK, | ||
headers = Headers.contentLength(message.length.toLong), | ||
data = HttpData.fromStream(ZStream.fromChunk(message)), // Encoding content using a ZStream | ||
) | ||
|
||
} | ||
} | ||
``` |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
33 changes: 33 additions & 0 deletions
33
zio-http/src/test/scala/zhttp/service/WebSocketServerSpec.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 |
---|---|---|
@@ -1 +1,34 @@ | ||
package zhttp.service | ||
|
||
import zhttp.http.Status | ||
import zhttp.internal.{DynamicServer, HttpRunnableSpec} | ||
import zhttp.service.server._ | ||
import zhttp.socket.{Socket, WebSocketFrame} | ||
import zio._ | ||
import zio.test.Assertion.equalTo | ||
import zio.test.TestAspect.timeout | ||
import zio.test._ | ||
|
||
object WebSocketServerSpec extends HttpRunnableSpec { | ||
|
||
private val env = | ||
EventLoopGroup.nio() ++ ServerChannelFactory.nio ++ DynamicServer.live ++ ChannelFactory.nio | ||
private val app = serve { DynamicServer.app } | ||
|
||
override def spec = suite("Server") { | ||
app.as(List(websocketSpec)).useNow | ||
}.provideCustomLayerShared(env) @@ timeout(10 seconds) | ||
|
||
def websocketSpec = suite("WebSocket Server") { | ||
suite("connections") { | ||
test("Multiple websocket upgrades") { | ||
val app = Socket.succeed(WebSocketFrame.text("BAR")).toHttp.deployWS | ||
val codes = ZIO | ||
.foreach(1 to 1024)(_ => app(Socket.empty.toSocketApp).map(_.status)) | ||
.map(_.count(_ == Status.SWITCHING_PROTOCOLS)) | ||
|
||
assertM(codes)(equalTo(1024)) | ||
} | ||
} | ||
} | ||
} |
f3bb049
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance Benchmark:
Concurrency: 256
Requests/sec: 927900.55