From 00d08514e6e85c7a8f874c83a06b11d1db7d3851 Mon Sep 17 00:00:00 2001 From: Grigorii Berezin Date: Tue, 7 May 2024 16:21:23 +0300 Subject: [PATCH 1/4] feat: add Routes#serve Method --- .../src/main/scala/example/HelloWorldAdvanced.scala | 2 +- zio-http/shared/src/main/scala/zio/http/Routes.scala | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala index c033117c13..f8178d6af5 100644 --- a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala +++ b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala @@ -35,7 +35,7 @@ object HelloWorldAdvanced extends ZIOAppDefault { val configLayer = ZLayer.succeed(config) val nettyConfigLayer = ZLayer.succeed(nettyConfig) - (Server.install(fooBar ++ app).flatMap { port => + ((fooBar ++ app).serve.flatMap { port => Console.printLine(s"Started server on port: $port") } *> ZIO.never) .provide(configLayer, nettyConfigLayer, Server.customized) diff --git a/zio-http/shared/src/main/scala/zio/http/Routes.scala b/zio-http/shared/src/main/scala/zio/http/Routes.scala index b8dba3b80f..a3a671f069 100644 --- a/zio-http/shared/src/main/scala/zio/http/Routes.scala +++ b/zio-http/shared/src/main/scala/zio/http/Routes.scala @@ -282,6 +282,10 @@ object Routes extends RoutesCompanionVersionSpecific { def singleton[Env, Err](h: Handler[Env, Err, (Path, Request), Response])(implicit trace: Trace): Routes[Env, Err] = Routes(Route.route(RoutePattern.any)(h)) + implicit class RouteOps[-Env, +Err <: Response](val routes: Routes[Env, Err]) extends AnyVal { + def serve: URIO[Env with Server, Int] = Server.install(routes) + } + private[http] final case class Tree[-Env](tree: RoutePattern.Tree[RequestHandler[Env, Response]]) { self => final def ++[Env1 <: Env](that: Tree[Env1]): Tree[Env1] = Tree(self.tree ++ that.tree) From 1db7993c63aba16d786c5c1e2b5cac979653b05f Mon Sep 17 00:00:00 2001 From: Grigorii Berezin Date: Tue, 7 May 2024 17:14:18 +0300 Subject: [PATCH 2/4] fix: correct serve method --- .../src/main/scala/example/HelloWorldAdvanced.scala | 6 ++---- zio-http/shared/src/main/scala/zio/http/Routes.scala | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala index f8178d6af5..e763dc4864 100644 --- a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala +++ b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala @@ -10,7 +10,7 @@ import zio.http.netty.NettyConfig.LeakDetectionLevel object HelloWorldAdvanced extends ZIOAppDefault { // Set a port - val PORT = 0 + val PORT = 58080 val fooBar = Routes( @@ -35,9 +35,7 @@ object HelloWorldAdvanced extends ZIOAppDefault { val configLayer = ZLayer.succeed(config) val nettyConfigLayer = ZLayer.succeed(nettyConfig) - ((fooBar ++ app).serve.flatMap { port => - Console.printLine(s"Started server on port: $port") - } *> ZIO.never) + (fooBar ++ app).serve .provide(configLayer, nettyConfigLayer, Server.customized) } } diff --git a/zio-http/shared/src/main/scala/zio/http/Routes.scala b/zio-http/shared/src/main/scala/zio/http/Routes.scala index a3a671f069..e7793b4819 100644 --- a/zio-http/shared/src/main/scala/zio/http/Routes.scala +++ b/zio-http/shared/src/main/scala/zio/http/Routes.scala @@ -283,7 +283,7 @@ object Routes extends RoutesCompanionVersionSpecific { Routes(Route.route(RoutePattern.any)(h)) implicit class RouteOps[-Env, +Err <: Response](val routes: Routes[Env, Err]) extends AnyVal { - def serve: URIO[Env with Server, Int] = Server.install(routes) + def serve: URIO[Env with Server, Nothing] = Server.serve(routes) } private[http] final case class Tree[-Env](tree: RoutePattern.Tree[RequestHandler[Env, Response]]) { self => From ac42dde4c15bb0ae6299e6278c17b89614c1fd4c Mon Sep 17 00:00:00 2001 From: Grigorii Berezin Date: Fri, 10 May 2024 09:43:41 +0300 Subject: [PATCH 3/4] feat: move to class method --- zio-http/shared/src/main/scala/zio/http/Routes.scala | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/zio-http/shared/src/main/scala/zio/http/Routes.scala b/zio-http/shared/src/main/scala/zio/http/Routes.scala index e7793b4819..264c33400f 100644 --- a/zio-http/shared/src/main/scala/zio/http/Routes.scala +++ b/zio-http/shared/src/main/scala/zio/http/Routes.scala @@ -193,6 +193,13 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s } } + /** + * A shortcut for `Server.install(routes) *> ZIO.never` + */ + def serve(implicit ev: Err <:< Response, trace: Trace): URIO[Env with Server, Nothing] = { + Server.serve(self.handleError(_.asInstanceOf[Response])) + } + def run( method: Method = Method.GET, path: Path = Path.root, @@ -282,10 +289,6 @@ object Routes extends RoutesCompanionVersionSpecific { def singleton[Env, Err](h: Handler[Env, Err, (Path, Request), Response])(implicit trace: Trace): Routes[Env, Err] = Routes(Route.route(RoutePattern.any)(h)) - implicit class RouteOps[-Env, +Err <: Response](val routes: Routes[Env, Err]) extends AnyVal { - def serve: URIO[Env with Server, Nothing] = Server.serve(routes) - } - private[http] final case class Tree[-Env](tree: RoutePattern.Tree[RequestHandler[Env, Response]]) { self => final def ++[Env1 <: Env](that: Tree[Env1]): Tree[Env1] = Tree(self.tree ++ that.tree) From 03762b70d70f23722ae86d6eedae595b11bcaeda Mon Sep 17 00:00:00 2001 From: Grigorii Berezin Date: Sun, 12 May 2024 21:49:34 +0300 Subject: [PATCH 4/4] fix: refactor for last features --- .../src/main/scala/example/HelloWorldAdvanced.scala | 3 ++- zio-http/shared/src/main/scala/zio/http/Routes.scala | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala index e763dc4864..f3823bf3ee 100644 --- a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala +++ b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala @@ -35,7 +35,8 @@ object HelloWorldAdvanced extends ZIOAppDefault { val configLayer = ZLayer.succeed(config) val nettyConfigLayer = ZLayer.succeed(nettyConfig) - (fooBar ++ app).serve + (fooBar ++ app) + .serve[Any] .provide(configLayer, nettyConfigLayer, Server.customized) } } diff --git a/zio-http/shared/src/main/scala/zio/http/Routes.scala b/zio-http/shared/src/main/scala/zio/http/Routes.scala index 264c33400f..815e443eb2 100644 --- a/zio-http/shared/src/main/scala/zio/http/Routes.scala +++ b/zio-http/shared/src/main/scala/zio/http/Routes.scala @@ -196,8 +196,12 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s /** * A shortcut for `Server.install(routes) *> ZIO.never` */ - def serve(implicit ev: Err <:< Response, trace: Trace): URIO[Env with Server, Nothing] = { - Server.serve(self.handleError(_.asInstanceOf[Response])) + def serve[Env1 <: Env](implicit + ev: Err <:< Response, + trace: Trace, + tag: EnvironmentTag[Env1], + ): URIO[Env1 with Server, Nothing] = { + Server.serve[Env1](self.handleError(_.asInstanceOf[Response])) } def run(