Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
khajavi committed Apr 28, 2024
1 parent a412a92 commit cc5b30e
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 109 deletions.
5 changes: 3 additions & 2 deletions zio-quickstart-restful-webservice/build.sbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
scalaVersion := "3.3.1"
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "2.0.19",
"dev.zio" %% "zio" % "2.0.22",
"dev.zio" %% "zio-json" % "0.6.2",
"dev.zio" %% "zio-http" % "3.0.0-RC2",
"dev.zio" %% "zio-http" % "3.0.0-RC6+36-d283e073-SNAPSHOT",
"io.getquill" %% "quill-zio" % "4.7.0",
"io.getquill" %% "quill-jdbc-zio" % "4.7.0",
"com.h2database" % "h2" % "2.2.224"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package dev.zio.quickstart

import dev.zio.quickstart.counter.CounterApp
import dev.zio.quickstart.download.DownloadApp
import dev.zio.quickstart.greet.GreetingApp
import dev.zio.quickstart.users.{InmemoryUserRepo, PersistentUserRepo, UserApp}
import dev.zio.quickstart.counter.CounterRoutes
import dev.zio.quickstart.download.DownloadRoutes
import dev.zio.quickstart.greet.GreetingRoutes
import dev.zio.quickstart.users.{InmemoryUserRepo, PersistentUserRepo, UserRoutes}
import zio._
import zio.http._

object MainApp extends ZIOAppDefault:
def run: ZIO[Environment with ZIOAppArgs with Scope, Throwable, Any] =
val httpApps = GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp()
def run =
Server
.serve(
httpApps.withDefaultErrorResponse
)
.serve(GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes())
.provide(
Server.defaultWithPort(8080),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ package dev.zio.quickstart.counter
import zio.http._
import zio.{Ref, ZIO, ZLayer}

/** An http app that:
* - Accepts `Request` and returns a `Response`
* - Does not fail
* - Requires the `Ref[Int]` as the environment
/** Collection of routes that:
* - Accept `Request` and returns a `Response`
* - Do not fail
* - Require the `Ref[Int]` as the environment
*/
object CounterApp:
def apply(): Http[Ref[Int], Nothing, Request, Response] =
Http.collectZIO[Request] {
case Method.GET -> Root / "up" =>
object CounterRoutes:
def apply(): Routes[Ref[Int], Nothing] =
Routes(
Method.GET / "up" -> handler {
ZIO.serviceWithZIO[Ref[Int]] { ref =>
ref
.updateAndGet(_ + 1)
.map(_.toString)
.map(Response.text)
}
case Method.GET -> Root / "down" =>
},
Method.GET / "down" -> handler {
ZIO.serviceWithZIO[Ref[Int]] { ref =>
ref
.updateAndGet(_ - 1)
.map(_.toString)
.map(Response.text)
}
case Method.GET -> Root / "get" =>
},
Method.GET / "get" -> handler {
ZIO.serviceWithZIO[Ref[Int]](ref =>
ref.get.map(_.toString).map(Response.text)
)
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package dev.zio.quickstart.download
import zio._
import zio.http._
import zio.stream.ZStream
import zio.schema.codec.JsonCodec.zioJsonBinaryCodec

/** An http app that:
* - Accepts a `Request` and returns a `Response`
* - May fail with type of `Throwable`
* - Does not require any environment
/** Collection of routes that:
* - Accept a `Request` and returns a `Response`
* - Do not require any environment
*/
object DownloadApp:
def apply(): Http[Any, Throwable, Request, Response] =
Http.collect[Request] {
object DownloadRoutes:
def apply(): Routes[Any, Nothing] =
Routes(
// GET /download
case Method.GET -> Root / "download" =>
Method.GET / Root / "download" -> handler {
val fileName = "file.txt"
http.Response(
status = Status.Ok,
Expand All @@ -23,10 +23,11 @@ object DownloadApp:
),
body = Body.fromStream(ZStream.fromResource(fileName))
)
},

// Download a large file using streams
// GET /download/stream
case Method.GET -> Root / "download" / "stream" =>
Method.GET / "download" / "stream" -> handler {
val file = "bigfile.txt"
http.Response(
status = Status.Ok,
Expand All @@ -40,4 +41,5 @@ object DownloadApp:
.schedule(Schedule.spaced(50.millis))
)
)
}
}
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.zio.quickstart.greet

import zio.http.*

/** Collection of routes that:
* - Accept a `Request` and return a `Response`
* - Do not fail
* - Do not use the environment
*/
object GreetingRoutes:
def apply(): Routes[Any, Nothing] =
Routes(
// GET /greet?name=:name
Method.GET / "greet" -> handler { (req: Request) =>
if (req.url.queryParams.nonEmpty)
Response.text(
s"Hello ${req.url.queryParams("name").map(_.mkString(" and "))}!"
)
else Response.badRequest("The name query parameter is missing!")
},

// GET /greet
Method.GET / "greet" -> handler(Response.text(s"Hello World!")),

// GET /greet/:name
Method.GET / "greet" / string("name") -> handler {( name: String, _: Request) =>
Response.text(s"Hello $name!")
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package dev.zio.quickstart.users

import java.util.UUID
import zio.json.*
import zio.schema._
import zio.schema.DeriveSchema._

case class User(name: String, age: Int)

object User:
given JsonEncoder[User] =
DeriveJsonEncoder.gen[User]
given JsonDecoder[User] =
DeriveJsonDecoder.gen[User]
given Schema[User] = DeriveSchema.gen[User]

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.zio.quickstart.users

import zio.*
import zio.http.*
import zio.schema.codec.JsonCodec.schemaBasedBinaryCodec

/** Collection of routes that:
* - Accept a `Request` and returns a `Response`
* - May fail with type of `Response`
* - Require a `UserRepo` from the environment
*/
object UserRoutes:
def apply(): Routes[UserRepo, Response] =
Routes(
// POST /users -d '{"name": "John", "age": 35}'
Method.POST / "users" -> handler { (req: Request) =>
for {
u <- req.body.to[User].orElseFail(Response.badRequest)
r <-
UserRepo
.register(u)
.mapBoth(
_ =>
Response
.internalServerError(s"Failed to register the user: $u"),
id => Response.text(id)
)
} yield r
},

// GET /users/:id
Method.GET / "users" / string("id") -> handler {
(id: String, _: Request) =>
UserRepo
.lookup(id)
.mapBoth(
_ => Response.internalServerError(s"Cannot retrieve user $id"),
{
case Some(user) =>
Response(body = Body.from(user))
case None =>
Response.notFound(s"User $id not found!")
}
)
},
// GET /users
Method.GET / "users" -> handler {
UserRepo.users.mapBoth(
_ => Response.internalServerError("Cannot retrieve users!"),
users => Response(body =Body.from(users))
)
}
)

0 comments on commit cc5b30e

Please sign in to comment.