Skip to content

Commit

Permalink
Add routes#serve method (#2829)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
GrigoriiBerezin and 987Nabil authored May 16, 2024
1 parent ac358fa commit 8510bfb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
}
11 changes: 11 additions & 0 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8510bfb

Please sign in to comment.