From 8510bfb914c5df886bcff25ec70008d3ce68aea7 Mon Sep 17 00:00:00 2001 From: Grigorii Berezin Date: Thu, 16 May 2024 17:33:39 +0300 Subject: [PATCH] Add routes#serve method (#2829) * feat: add Routes#serve Method * fix: correct serve method * feat: move to class method * fix: refactor for last features --------- Co-authored-by: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> --- .../src/main/scala/example/HelloWorldAdvanced.scala | 7 +++---- zio-http/shared/src/main/scala/zio/http/Routes.scala | 11 +++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala index c033117c13..f3823bf3ee 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,8 @@ object HelloWorldAdvanced extends ZIOAppDefault { val configLayer = ZLayer.succeed(config) val nettyConfigLayer = ZLayer.succeed(nettyConfig) - (Server.install(fooBar ++ app).flatMap { port => - Console.printLine(s"Started server on port: $port") - } *> ZIO.never) + (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 b8dba3b80f..815e443eb2 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,17 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s } } + /** + * A shortcut for `Server.install(routes) *> ZIO.never` + */ + 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( method: Method = Method.GET, path: Path = Path.root,