By Alvin Alexander. Last updated: March 31, 2024
As a brief note today, this is a very little ZIO HTTP Server application. I’m sharing it here because it’s currently a fairly simple example, and also because I show how to include the necessary ZIO dependencies using Scala-CLI:
//> using scala "3"
//> using lib "dev.zio::zio::2.0.21"
//> using lib "dev.zio::zio-http::3.0.0-RC4"
//> using lib "dev.zio::zio-json::0.6.2"
package foo
/**
Helpful ZIO HTTP URLs:
----------------------
- https://zio.dev/zio-http/
- https://zio.github.io/zio-http/docs/v1.x/getting-started/
- https://zio.dev/zio-http/getting-started/
*/
// SCALA-CLI NOTES:
// ----------------
// Set up your IDE:
// scala-cli setup-ide .
// Run me like this:
// scala-cli ThisFilename.scala
import zio.*
import zio.ZIOAppDefault
import zio.http.*
object ZioHttpServer extends ZIOAppDefault:
// ZIO HTTP 101:
// val app = Handler.text("Hello, world").toHttpApp
val app =
Routes(
Method.GET / "todo" -> {
Handler.text(s"1. wake up\n2. make coffee")
},
Method.GET / "blog" / "1" ->
handler(Response.text("1st blog post"))
).toHttpApp
def run = Server.serve(app)
.provide(Server.defaultWithPort(8888))
// OR: provide(Server.default) // port 8080
In the future I’ll be writing much more about ZIO and ZIO HTTP, but for today, this is a little ZIO HTTP server starter application.
Running the ZIO HTTP server app with Scala-CLI
Before I go, note that you start and run this ZIO HTTP server application like this:
scala-cli ZioHttpServer.scala
This current source code will listen on Port 8888, and once it’s running, you can access the HTTP service with a curl
command like this:
curl -i localhost:8090/todo
In summary, if you are interested in seeing a small ZIO HTTP server application, and also how to see it work with Scala-CLI, I hope this example is helpful.