Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade ZIO‌ HTTP #49

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions zio-quickstart-restful-webservice-configurable-app/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ scalaVersion := "2.13.8"
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "2.0.19",
"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 All @@ -12,4 +12,4 @@ libraryDependencies ++= Seq(
"dev.zio" %% "zio-config-magnolia" % "4.0.0-RC16"
)

resolvers += Resolver.sonatypeRepo("public")
resolvers ++= Resolver.sonatypeOssRepos("snapshots")
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.zio.quickstart

import dev.zio.quickstart.config.HttpServerConfig
import dev.zio.quickstart.counter.CounterApp
import dev.zio.quickstart.download.DownloadApp
import dev.zio.quickstart.greet.GreetingApp
import dev.zio.quickstart.users.{InmemoryUserRepo, UserApp, UserRepo}
import dev.zio.quickstart.counter.CounterRoutes
import dev.zio.quickstart.download.DownloadRoutes
import dev.zio.quickstart.greet.GreetingRoutes
import dev.zio.quickstart.users.{InmemoryUserRepo, UserRoutes}
import zio._
import zio.config.typesafe.TypesafeConfigProvider
import zio.http._
Expand Down Expand Up @@ -34,9 +34,10 @@ object MainApp extends ZIOAppDefault {
)

def run = {
val httpApp = GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp()
(Server
.serve(httpApp.withDefaultErrorResponse)
.install(
GreetingRoutes() ++ DownloadRoutes() ++ CounterRoutes() ++ UserRoutes()
)
.flatMap(port =>
Console.printLine(s"Started server on port: $port")
) *> ZIO.never)
Expand Down

This file was deleted.

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)
)
}
}
)
}
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,5 +41,6 @@ object DownloadApp {
.schedule(Schedule.spaced(50.millis))
)
)
}
}
)
}

This file was deleted.

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!")
}
)
}
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]
}

This file was deleted.

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))
)
}
)
}
4 changes: 3 additions & 1 deletion zio-quickstart-restful-webservice-custom-logger/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ scalaVersion := "2.13.8"
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "2.0.19",
"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",
"dev.zio" %% "zio-logging" % "2.1.15",
"dev.zio" %% "zio-logging-slf4j" % "2.1.15",
"org.slf4j" % "slf4j-simple" % "2.0.9"
)

resolvers ++= Resolver.sonatypeOssRepos("snapshots")
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
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, UserApp, UserRepo}
import dev.zio.quickstart.users.{InmemoryUserRepo, UserRoutes}
import zio._
import zio.http._
import zio.logging.LogFormat
import zio.logging.backend.SLF4J

object MainApp extends ZIOAppDefault {
override val bootstrap: ZLayer[Any, Nothing, Unit] =
SLF4J.slf4j(LogLevel.All, LogFormat.colored)
SLF4J.slf4j(LogFormat.colored)

def run = {
val httpApps = GreetingApp() ++ DownloadApp() ++ CounterApp() ++ UserApp()
Server
.serve(
httpApps.withDefaultErrorResponse
)
.serve(UserRoutes())
.provide(
Server.defaultWithPort(8080),

Expand Down
Loading
Loading