Skip to content

usommerl/graalnative4s

Repository files navigation

graalnative4s

build codecov Scala Steward badge

This is a showcase for a combination of purely functional Scala libraries that can be used with GraalVM native-image without much effort. It employs http4s for general server functionality, circe for JSON processing, ciris to load runtime configuration, tapir to describe HTTP endpoints and odin for logging. Applications that were built with native-image have beneficial properties such as a lower memory footprint and fast startup. This makes them suitable for serverless applications.

Build

Use sbt docker to build a docker image with the native image binary. You don't need to install anything besides docker and sbt, the build process downloads all required GraalVM tooling. The created image will be as minimal as possible by using a multi-stage build.

You can create an even smaller image by utilizing UPX compression. Use the UPX_COMPRESSION environment variable at build time to specify the compression level. Please note that while this reduces the size of the image significantly it also has an impact on startup performance and memory consumption.

Example: export UPX_COMPRESSION="--best"; sbt docker

Deploy

This repository contains a workflow that will deploy the created image to Google Cloud Run. You could also use the button below to deploy it to your own GCP account.

Run on Google Cloud

Try

The most recent version of this small example is online here: https://graalnative4s.usommerl.dev

Acknowledgements & Participation

I have taken a lot of inspiration and knowledge from this blog post by James Ward. You should check out his hello-uzhttp example. Another project that helped me to connect the dots regarding native-image configuration was vasilmkd/docker-stats-monitor. Suggestions and contributions to this repository are welcome!