Search⌘ K
AI Features

Starting the Application

Explore initializing a pure functional HTTP API in Scala by setting up the database, configuring routes with http4s, and managing the application lifecycle using Cats effect IOApp. Understand how to wire components together and execute side effects safely within a functional paradigm.

Main application

Within our main entry point, we initialize all needed components and wire them together. We’ll step through each part in this section.

The first thing you’ll notice is that we use the IOApp provided by the Cats effect library.

Scala
object Pure extends IOApp {
@SuppressWarnings(Array("org.wartremover.warts.Any"))
def run(args: List[String]): IO[ExitCode] = ???
}

We need to suppress a warning from wartremover (Line 2) here as well.

Database initialization

Let’s continue to initialize the database connection.

Scala
val migrator: DatabaseMigrator[IO] = new FlywayDatabaseMigrator
val program = for {
(apiConfig, dbConfig) <- IO {
val cfg = ConfigFactory.load
(loadConfigOrThrow[ApiConfig](cfg, "api"),
loadConfigOrThrow[DatabaseConfig](cfg, "database"))
}
ms <- migrator.migrate(dbConfig.url, dbConfig.user, dbConfig.pass)
tx = Transactor
.fromDriverManager[IO](dbConfig.driver,
dbConfig.url,
dbConfig.user,
dbConfig.pass)
repo = new DoobieRepository(tx)

We create our database migrator explicitly wired to the IO data type.

Now, we start with a for comprehension in which we load our ...