Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishi Raj Jain committed Nov 23, 2023
1 parent 2f8a343 commit ddf6f92
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 42 deletions.
7 changes: 7 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ lazy val aggregatedProjects: Seq[ProjectReference] =
zioHttpBenchmarks,
zioHttpCli,
zioHttpExample,
zioHttpReactExample,
zioHttpTestkit,
docs,
)
Expand Down Expand Up @@ -237,6 +238,12 @@ lazy val zioHttpExample = (project in file("zio-http-example"))
.settings(libraryDependencies ++= Seq(`jwt-core`))
.dependsOn(zioHttp, zioHttpCli)

lazy val zioHttpReactExample = (project in file("zio-http-react-example"))
.settings(stdSettings("zio-http-react-example"))
.settings(publishSetting(false))
.settings(libraryDependencies ++= Seq("dev.zio" %% "zio-http" % ZioVersion))
.dependsOn(zioHttp, zioHttpCli)

lazy val zioHttpTestkit = (project in file("zio-http-testkit"))
.enablePlugins(Shading.plugins(): _*)
.settings(stdSettings("zio-http-testkit"))
Expand Down
37 changes: 23 additions & 14 deletions docs/examples/advanced/fullstack-with-react-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,31 @@ import zio.stream.ZStream
import java.io.File
import java.nio.file.Paths

object HelloWorld extends ZIOAppDefault {
object ReactHelloWorld extends ZIOAppDefault {
// Create the build relative directory path
private val buildDirectory = "../../react/build"

// Create HTTP route
val app = Routes(
Method.GET -> Root / "api" / "hello" => Handler.text("Hello World!").toHttp,

// Uses netty's capability to write file content to the Channel
// Content-type response headers are automatically identified and added
// Adds content-length header and does not use Chunked transfer encoding
Method.GET -> Root => Handler.fromFile(new File(s"$buildDirectory/index.html")),
Method.GET -> "" /: file => Handler.fromFile(new File(s"$buildDirectory/$file")),
).sandbox.toHttpApp
private val buildDirectory = "src/main/scala/react_example/react/build"

val app: HttpApp[Any] = Routes.singleton {
handler { (_: Path, req: Request) => {
if (req.url.path.toString == "/api/hello") {
Response.text("Hello World")
}
if (req.url.path.toString == "/") {
val file = new File(s"$buildDirectory/index.html")
val length = Headers(Header.ContentLength(file.length()))
Response(headers = length, body = Body.fromFile(file))
}
else {
val tmp = req.url.path.toString
val file = new File(s"$buildDirectory/$tmp")
val length = Headers(Header.ContentLength(file.length()))
Response(headers = length, body = Body.fromFile(file))
}
}
}
}.toHttpApp

// Run it like any simple app
override val run = Server.serve(app.withDefaultErrorResponse).provide(Server.default)
val run = Server.serve(app).provide(Server.default)
}
```
27 changes: 0 additions & 27 deletions zio-http-react-example/src/main/scala/example/app.scala

This file was deleted.

1 change: 0 additions & 1 deletion zio-http-react-example/src/main/scala/example/build.sbt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package react_example

import zio._
import zio.http._
import zio.stream.ZStream

import java.io.File
import java.nio.file.Paths

object ReactHelloWorld extends ZIOAppDefault {
// Create the build relative directory path
private val buildDirectory = "src/main/scala/react_example/react/build"

val app: HttpApp[Any] = Routes.singleton {
handler { (_: Path, req: Request) => {
if (req.url.path.toString == "/api/hello") {
Response.text("Hello World")
}
if (req.url.path.toString == "/") {
val file = new File(s"$buildDirectory/index.html")
val length = Headers(Header.ContentLength(file.length()))
Response(headers = length, body = Body.fromFile(file))
}
else {
val tmp = req.url.path.toString
val file = new File(s"$buildDirectory/$tmp")
val length = Headers(Header.ContentLength(file.length()))
Response(headers = length, body = Body.fromFile(file))
}
}
}
}.toHttpApp

// Run it like any simple app
val run = Server.serve(app).provide(Server.default)
}

0 comments on commit ddf6f92

Please sign in to comment.