Skip to main content
Version: 2.0.x

CLI Client-Server Examples

zio-http-example/src/main/scala/example/ConcreteEntity.scala
package example

import zio._

import zio.http._

/**
* Example to build app on concrete entity
*/
object ConcreteEntity extends ZIOAppDefault {
// Request
case class CreateUser(name: String)

// Response
case class UserCreated(id: Long)

val user: Handler[Any, Nothing, CreateUser, UserCreated] =
Handler.fromFunction[CreateUser] { case CreateUser(_) =>
UserCreated(2)
}

val app: HttpApp[Any] =
user
.contramap[Request](req => CreateUser(req.path.encode)) // Http[Any, Nothing, Request, UserCreated]
.map(userCreated => Response.text(userCreated.id.toString)) // Http[Any, Nothing, Request, Response]
.toHttpApp

// Run it like any simple app
val run =
Server.serve(app).provide(Server.default)
}