Initial App Build
Learn how to create an API only based Ruby on Rails application, including the presets you can integrate into the app generator.
Guide Tasks
  • Read Tutorial

Creating an API only Rails App

Since the application that we'll be building in this course is a microservice and won't have a front end, we're going to leverage a custom Rails application generator that is more lightweight and will give us an API only based application. Let's run this in the terminal:

rails new message_service --api -T --database=postgresql

What exactly is different about this type of rails application generator? Essentially it configures the application to skip view based activities, such as view generators, cookie support, and related functionality. Since our microservice is going to be an API for other applications this fits our needs perfectly and it will make the file structure less cluttered.

Running the generator will create the application, running rake db:create && rake db:migrate will setup our Postgres database. Opening up the application in a text editor will reveal that the overall file structure is the same as a normal application. It's for this reason why I personally prefer building microservice applications with this api flag as opposed to using a framework such as Sinatra. I like it because I don't have to pay a switching cost for performing tasks on different frameworks throughout the day. Whether I'm working on a front end application or an API, the high level coding fundamentals will be the same.

Since this is an API based application, let's test to ensure that it was created properly using a different method than normal. Start up the rails server normally:

rails s

And now open up a new terminal tab and run:

curl localhost:3000

large

This is leveraging cURL command line tool to call our local server. And as you'll see, it returned the running application. We'll be using cURL quite a bit throughout this build to test API calls in our microservice.

Resources