Skip to main content
Version: 2.0.x

Server

This section describes, ZIO HTTP Server and different configurations you can provide while creating the Server

Start a ZIO HTTP Server with default configurations

import zio.http._
import zio._

def app: HttpApp[Any] = ???
Server.serve(app).provide(Server.default)
// scala.NotImplementedError: an implementation is missing
// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
// at repl.MdocSession$MdocApp.app(server.md:14)
// at repl.MdocSession$MdocApp$$anonfun$1.apply(server.md:21)
// at repl.MdocSession$MdocApp$$anonfun$1.apply(server.md:21)

A quick shortcut to only customize the port is Server.defaultWithPort:

Server.serve(app).provide(Server.defaultWithPort(8081))
// scala.NotImplementedError: an implementation is missing
// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
// at repl.MdocSession$MdocApp.app(server.md:14)
// at repl.MdocSession$MdocApp$$anonfun$2.apply(server.md:31)
// at repl.MdocSession$MdocApp$$anonfun$2.apply(server.md:31)

Or to customize more properties of the default configuration:

Server.serve(app).provide(
Server.defaultWith(
_.port(8081).enableRequestStreaming
)
)
// scala.NotImplementedError: an implementation is missing
// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
// at repl.MdocSession$MdocApp.app(server.md:14)
// at repl.MdocSession$MdocApp$$anonfun$3.apply(server.md:41)
// at repl.MdocSession$MdocApp$$anonfun$3.apply(server.md:41)

Start a ZIO HTTP Server with custom configurations.

The live layer expects a Server.Config holding the custom configuration for the server.

Server
.serve(app)
.provide(
ZLayer.succeed(Server.Config.default.port(8081)),
Server.live
)
// scala.NotImplementedError: an implementation is missing
// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
// at repl.MdocSession$MdocApp.app(server.md:14)
// at repl.MdocSession$MdocApp$$anonfun$4.apply(server.md:56)
// at repl.MdocSession$MdocApp$$anonfun$4.apply(server.md:57)

The configured layer loads the server configuration using the application's ZIO configuration provider, which is using the environment by default but can be attached to a different backends using the ZIO Config library.

Server
.serve(app)
.provide(
Server.configured()
)
// scala.NotImplementedError: an implementation is missing
// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
// at repl.MdocSession$MdocApp.app(server.md:14)
// at repl.MdocSession$MdocApp$$anonfun$6.apply(server.md:71)
// at repl.MdocSession$MdocApp$$anonfun$6.apply(server.md:72)

In order to customize Netty-specific properties, the customized layer can be used, providing not only Server.Config but also NettyConfig.