-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from khajavi/update-zio-quickstart-restful
Upgrade ZIO HTTP
- Loading branch information
Showing
45 changed files
with
575 additions
and
763 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
34 changes: 0 additions & 34 deletions
34
...ul-webservice-configurable-app/src/main/scala/dev/zio/quickstart/counter/CounterApp.scala
This file was deleted.
Oops, something went wrong.
27 changes: 15 additions & 12 deletions
27
...v/zio/quickstart/counter/CounterApp.scala → ...io/quickstart/counter/CounterRoutes.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,33 +1,36 @@ | ||
package dev.zio.quickstart.counter | ||
|
||
import zio.http._ | ||
import zio.{Ref, ZIO} | ||
import zio._ | ||
|
||
/** 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) | ||
) | ||
} | ||
} | ||
) | ||
} |
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
28 changes: 0 additions & 28 deletions
28
...ful-webservice-configurable-app/src/main/scala/dev/zio/quickstart/greet/GreetingApp.scala
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
...-webservice-configurable-app/src/main/scala/dev/zio/quickstart/greet/GreetingRoutes.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,31 @@ | ||
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!") | ||
} | ||
) | ||
} |
9 changes: 3 additions & 6 deletions
9
...rt-restful-webservice-configurable-app/src/main/scala/dev/zio/quickstart/users/User.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,13 +1,10 @@ | ||
package dev.zio.quickstart.users | ||
|
||
import java.util.UUID | ||
import zio.json._ | ||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
case class User(name: String, age: Int) | ||
|
||
object User { | ||
implicit val encoder: JsonEncoder[User] = | ||
DeriveJsonEncoder.gen[User] | ||
implicit val decoder: JsonDecoder[User] = | ||
DeriveJsonDecoder.gen[User] | ||
implicit val schema: Schema[User] = | ||
DeriveSchema.gen[User] | ||
} |
49 changes: 0 additions & 49 deletions
49
...restful-webservice-configurable-app/src/main/scala/dev/zio/quickstart/users/UserApp.scala
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
...tful-webservice-configurable-app/src/main/scala/dev/zio/quickstart/users/UserRoutes.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 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)) | ||
) | ||
} | ||
) | ||
} |
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
12 changes: 3 additions & 9 deletions
12
...ickstart-restful-webservice-custom-logger/src/main/scala/dev/zio/quickstart/MainApp.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
Oops, something went wrong.