Building a Rails 5 API Microservice
Walk through a step by step guide for building a Ruby on Rails API microservice application.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this section of the course we're going to go and build our next microservice application. So we're going to create our proposal microservice and this is going to be generated the same way that we built the other one. So if I type

ls

You can see that this time I'm not going to make the same mistake twice.

large

I'm going to put this application in the right directory where it belongs.
So I'm going to say rails new and this one is if I'm following my naming convention should be freelance_camp_proposal and then remember we want this to be an API only application so we're going to pass the flag of --api then -T to bypass the tests then I'll do -d for the database so that we use postgresql by default.

rails new freelance_camp_proposal --api -T -d postgresql

And this will generate our application for us. So our application is ready to go. And now we can change into it so we'll type

cd freelance_camp_proposal

Now inside of here I'll run

bundle install

Just to make sure it has everything. And I'm also going to put in our cors update. So here remember cors allows us to have all of our API connections into our new microservice so I'm going to open up the Gemfile and right down here on line 21 uncomment gem 'rack-cors. Then I am going to run

bundle install 

again. This brings rack-cors to us and from here I can go into the config/initializers/cors.rb and we want to uncomment all this code then change 'example.com' to a '*', hit save and we're good to go.

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*'
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

and we've created our next application which is our proposal microservice. In the next guide, we're going to create our scaffold that's going to generate all of the various items that we need for our microservice.

Resources