ZIO HTTP: A collection of Route examples
I try not to do this too often, but here’s a currently-incomplete list of ways to define ZIO HTTP routes. I start with some of the simplest examples, and then make them more complicated as I go along:
I try not to do this too often, but here’s a currently-incomplete list of ways to define ZIO HTTP routes. I start with some of the simplest examples, and then make them more complicated as I go along:
Without much explanation, the purpose of the following ZIO 2 + Scala 3 code is to show the absolute basics of a working ZIO HTTP + MySQL application. In this case I use the Scalikejdbc library, but as you can see from the code, you can use any Scala, Java, or JVM SQL library you want.
This is almost the “simplest possible”, “Hello world” application that shows all these features.
Very soon I will have a much more complete example including the use of packages, configuration, repository, service, api, and logging, in my free video courses at LearnScala.dev.
Here’s the code:
On May 24, 2013, I finished with the last hardcopy chapters of the Scala Cookbook. I put all of the chapters next to the paper shredder as a way to show what I had just done. The final edits would be finished with a copywriter over the next several weeks, and I signed off on the final edition while I was at Virginia Beach.
GraphQL FAQ: What are the benefits of GraphQL, and how does it compare to competing technologies?
This is another blog post in my series of “Conversations with robots.” In this post we look at GraphQL, and the pros and cons of how it compares to REST and gRPC.
As a quick note today, here’s a list of my currently-free books and online video courses about the Scala programming language and functional programming (as of October, 2024):
May, 2024: Here’s a summary of my current free online Scala and functional programming training courses:
If you’re interested in 100% Free online Scala and FP video training courses, I hope these online video courses are helpful.
I know that a lot of people devalue products when they’re free or inexpensive, and I know this sounds corny, but I always hope that somewhere out there in the world there’s a programmer/developer that works hard, but for one reason or another can’t quite grok Scala and/or functional programming, but then they find one of my books, and the light bulb goes on over their head. And then they start writing better code, and they’re happier with their work, other people are happier with their work, and they make a little more money than they might have made otherwise.
With that in mind, I’ve lowered the price of my Kindle books to just $2.99 (USD) for the 2024 holiday season. Here’s Learn Functional Programming The Fast Way, and here’s Learn Scala 3 The Fast Way.
Also, please note that the PDF versions of these books are FREE.
Enjoy, and I hope they help. :)
Scala 3 FAQ: How does this ZIO HTTP and ZIO JSON code works, specifically the derives JsonEncoder
portion of the code:
case class Greeting(message: String) derives JsonEncoder
In Scala 3, the derives
keyword is used to automatically generate implementations for type classes. Here’s how this works in the context of ZIO JSON.
As a brief note, if you need to debug a ZIO HTTP Request
value, I just created this function to return its values as a String
, and it appears to work properly:
As a brief note, my Mac/macOS system disk was pretty full, so I started looking at things I could delete easily. One thing I found is that the Scala-CLI/Coursier cache is location in this Mac directory:
/Users/al/Library/Caches/Coursier
I don’t know yet if it’s safe to delete anything in that directory/folder, but it’s using 7.2GB of space, so I’m about to look into it.
UPDATE: Here is information on deleting the scala-cli/Coursier cache.
Note that I deleted everything in that Coursier directory, and after that NONE of my Java and Scala tools worked, and I had to manually run commands like this to get Java working again:
eval "$(cs java --jvm 11 --env)"
and then later I had problems with scala-cli
and had to run this command to kill bloop
:
scala-cli --power bloop exit
Here’s a comprehensive guide to implementing retry logic in ZIO 2 applications, using various scheduling strategies.
Please note that I haven’t double-checked that all of these examples compile as-is, but I do demonstrate many of these in my free Scala and ZIO 2 training videos. (I also added a complete working example at the end.)
April, 2024 Update: This ZIO cheatsheet is currently being updated to ZIO 2.x, but it’s still a work in progress.
If you want a good cheat sheet right now, see this one on github. I’m creating my own as I learn ZIO and read the ZIOnomicon book. During the learning process I find that it’s much better to create your own by hand, that way you get something that’s meaningful to you.
Note that almost all of these initial examples come from the ZIOnomicon book and the video that I link to later.
Here’s a link to the ZIO Scaladoc. That’s for the companion object, and this link is for the companion trait.
In case these two examples disappear from the ZIO JDBC repository, I have made a copy of them here.
(I say that because earlier this week (late October, 2024) I was told the the ZIO JDBC library is no longer maintained, and then I was also told that they just found a new maintainer.)
That being said, here are the ZIO-JDBC examples. This first one is called Basic.scala
:
Here’s a brief Scala ZIO HTTP “Hello, world” example from that repository, followed by a description of the code and “key points” to know about the example:
import zio.*
import zio.http.*
object HelloWorld extends ZIOAppDefault:
// responds with plain text
val homeRoute =
Method.GET / Root -> handler(Response.text("Hello, World!"))
// responds with JSON
val jsonRoute =
Method.GET / "json" -> handler(Response.json("""{"greetings": "Hello World!"}"""))
// create HTTP route
val app = Routes(homeRoute, jsonRoute)
// run it like any simple app
override val run = Server.serve(app)
.provide(Server.default)
I just added free videos on ZIO 2 fibers and parallel/concurrent combinator functions to my Scala/FP video training course website.
Just a periodic reminder today that here’s a link to my free Scala and functional programming training video courses at LearnScala.dev.
If you haven’t seen them yet, here’s a link to Alvin Alexander’s free Scala 3 and Functional Programming video courses. The functional programming course shown in this image was just released today, September, 8, 2024.
October, 2024, and my new free “Functional Programming Fundamentals with ZIO” video course is now available!
Functional Programming FAQ: What are the benefits of an Effect System, like ZIO?
I’m currently planning a video on “The benefits of an Effect System in computer programming” on my free Scala and functional programming videos website, but in case I don’t get around to making that video, here are the slides I have put together.
Also, if you’d like to see a video that is somewhat similar, here’s my popular “What is ZIO 2?” video on YouTube.
If you haven’t heard of Effects or an Effects System, I wrote about them a long time ago here. Also, in book, Effect-Oriented Programming, the authors describe them this way:
In this fast-paced video I show what the ZIO 2 library is in the fastest way I know how. I show the benefits of effects and effect systems, and specifically how ZIO is the new “Functional Programming, Simplified.”