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

Add routes#serve method #2829

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
Loading