Hacking with http4s, doobie and cats-effects

Lambda Ops

Sep 25, 2019 · 3 min read

Http4s and doobie

http4s is a type safe, functional, HTTP streaming library for Scala. It built based on cats library. We can build pure functional REST applications with full control of side-effects with http4s. doobie is a pure functional JDBC layer for scala. It also built with with cats library.

On my previous post I have discussed about doobie and cats-effects. In this post I’m gonna discuss about building REST application with http4s, doobie and cats-effects. All the source codes which relates to this post available on gitlab. Please clone the repo and continue the post.

Sbt dependency

I’m using IntelliJ Idea as my IDE to work with Scala applications. I need to create sbt project and add the build.sbt dependency file with http4s, doobie and other dependencies. Following is the build.sbt dependency file.

Load config

I’m using cats-effects IO monads to build this application. To load the configurations(database config, server config) with IO monads I have used circe-config library. Following is the way to parse configurations into cats-effects IO.

Database config

To connect to the database, I need to create an instance of a doobie transactor. In here I’m using HikariTransactor which built based on HikariCP database connection pooling library. Following are the functions to build HikariTransactor and bootstrap the database. All of these functions built with cats-effects IO monads.

Database model

In this application I’m dealing with a model, Account. Account models are saving on mysql database table, mystiko.accounts. Following is the Account model structure and database table structure.

Doobie queries

doobie queries are written directly in SQL. Following are the doobie queries that I have used to perform the CURD operations with the data on accounts table.

Execute queries

To execute above queries I need to create data layer functions. I have created DB repository(AccountRepo) with following create, update, delete and search functions.

Http4s routes

Next thing is create HTTP interface(HttpRoutes) to interact with AccountRepo functions. Following is the way to define these endpoints in terms of http4s syntax.

Bind server

To make this REST API live I need to create a server and bind the these routes to it(in http4s routes do not handle any requests until we explicitly associate them with running server). Following is the way to bind routes to http4s native server blaze. I have used cats-effects IO monads in here as well.

Run database

I’m using mysql as the database backend. In order to test this application I need to run the mysql and create a database on it. Following is the way to do that with with docker.

Test application

When we run this application it will starts four REST API end points. I can test these end points with following curl requests.



Have less, be more


Written by


Ego = 1/Knowledge



Have less, be more

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade