Let’s Build a Simple Hanami App

The best way to get a taste of what Hanami is all about is to roll up our sleeves and build something with it. The app we are building is a very simple blog app.

Using this example, you will learn how a Hanami 2.0 app is structured.

Prerequisites

Before we get started, make sure you have the following:

  • Ruby installed – Ensure you have Ruby 2.3+ installed on your local development machine. We recommend using a Ruby version manager, such as RVM or rbenv.
  • Bundler installed – Make sure to have the gem manager Bundler locally installed.
  • Local SQLite 3+ installation – Although you can use the Postgres database, in this example, we’ll be using an SQLite 3+ database. In case it’s not already installed, use Bundler to install it with bundle install sqlite3.
  • NodeJS – Node 16+ installed (as of writing this article). You can use Node version manager (NVM) to manage different Node versions.
  • Overmind installed – Overmind is a process manager that lets you run several processes from a single terminal. Also, make sure you have tmux, a terminal session manager, installed as it’s required by Overmind.

Important note: Hanami 2.0 is still in alpha and under heavy development at the time of this writing, so things may break.

Step 1: Setup

With the Hanami CLI for version 2 under heavy development, we’ll use the version 2.0 starter template available here. The completed sample app we’ll build is available here.

To follow along, go ahead and clone the repo and rename it to ‘hanami2-blog-app’.

git clone https://github.com/hanami/hanami-2-application-template.git hanami2-blog-app

Next, in the terminal, switch to the project folder and run bundle to install the required gems.

Then, go ahead and rename all instances of the original template project name to the name of your new app, “hanami2-blog-app” in this case, using the following command:

./bin/install hanami2-blog-app

With this done, run the following in the terminal:

./script/setup

This will setup a couple of things within the project, including creating the app database.

Step 2: Run the App

Let’s now run our example app just to see what a basic Hanami 2 app looks like. Go ahead and run the following command:

./script/server

Now, if everything went as planned, a visit to localhost:3000 should show your app running.

Before we dive into creating our app’s blog functionality, it would be prudent to have an overview of how a Hanami 2 app is structured, especially if you are coming from a Rails background since it has its own way of doing things.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *