/zio-akka-quickstart.g8

A Giter8 template for a basic Scala application build using ZIO, Akka HTTP and Slick!

Primary LanguageScalaCreative Commons Zero v1.0 UniversalCC0-1.0

Scala CI

zio-akka-quickstart

A Giter8 template for a basic Scala application build using ZIO, Akka HTTP and Slick!

This template integrates ZIO with Akka HTTP and Slick, so that you can seamlessly use all power of ZIO in a familiar environment of battle tested akka-http routes and slick query DSL.

It also leverages ZLayer and ZManaged to build dependency graph in a very visible, type-safe and resource-safe way.

Setting up the project

sbt new ScalaConsultants/zio-akka-quickstart.g8
# then follow interactive process to choose project name and other parameters

Run tests

cd <project-name>
sbt test

Launch and interact

sbt run

# create an item
curl --request POST \
  --url http://localhost:8080/items \
  --header 'content-type: application/json' \
  --data '{
	"name":"BigMac",
	"price": 10.0
}'

# get all items
curl --request GET \
  --url http://localhost:8080/items

Check out more routes in Api.scala

Components and libraries

This sample app has several key components:

  • ItemRepository, which is quite a regular slick repo, with the only twist - it works with ZIO and not Futures. Backed up by an H2 database and Hikari connection pool.
  • Api - pretty standard akka-http CRUD endpoint, also powered by ZIO.
  • interop - package, where all the integration magic is happening.
  • ApplicationService - a more higher level service, that works with different error type and uses ZIO environment.
  • Boot - wiring all the components together using ZLayer.
  • ApiSpec - akka-http endpoint spec using zio-test.

Additional libraries used:

  • caliban for an optional GraphQL endpoint.
  • play-json for JSON processing.
  • zio-logging for logging.
  • zio-config for typesafe configuration.
  • zio-test for testing.

GraphQL

During initialization step, there is a possibility to add GraphQL endpoint managed by Caliban library. It adds two endpoints to the api: /api/graphql which is responsible for GraphQL queries and /graphiql with simple GraphiQL console to play with the API.

To try out this feature open the browser http://localhost:8080/graphiql

Template license

Written in 2020 by Scalac Sp. z o.o..

To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this template to the public domain worldwide. This template is distributed without any warranty. See http://creativecommons.org/publicdomain/zero/1.0/.