Webservice for system info: server part

By | 01/09/2014


The purpose is to build a webservice to store system information such as private and public IP address, computername etc. We will build a Sinatra app with a sqlite database for development purposes and a Postgres database for production purposes as we will deploy it to Heroku when finished. If it’s the first time you are deploying a Sinatra application to Heroku, you can first follow this tutorial to get up and running.

The model and application

We will create a model that stores the relevant data such the IP addresses and the computernames. For lack of a better name, I will call it “IP”:

class IP
  include DataMapper::Resource  
  property :id,           Serial,	key: true, unique_index: true
  property :public_ip,    String,	length: 1..16
  property :private_ip,   String,	length: 1..16
  property :systemname,	  String,	length: 1..100
  property :created_at,   DateTime
  property :updated_at,   DateTime

In the Sinatra routes file, we will expose the basic CRUD operations to create, view and delete entries:

  get "/ip" do
    format_response(IP.all, request.accept)

  get "/ip/:id" do
    ip ||= IP.get(params[:id]) || halt(404)
    format_response(ip, request.accept)

  post "/ip" do
    body = JSON.parse request.body.read
    ip = IP.create(
      private_ip: body['private'],
      public_ip:  body['public'],
      systemname: body['systemname'],
      created_at: Time.now,
      updated_at: Time.now
  status 201
  format_response(ip, request.accept)
  delete '/ip/:id' do
    ip ||= IP.get(params[:id]) || halt(404)
    halt 500 unless ip.destroy

The full source can be found on Github here

Deploying to Heroku

We will create an Heroku app called publicprivateip.

$ git init
$ git add .
$ git commit -m "First commit"
[master (root-commit) 29708e9] First commit
 28 files changed, 9972 insertions(+)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 Rakefile
$ heroku create publicprivateip 
Creating publicprivateip... done, stack is cedar

And attach also a PostgreSQL database to the application:

$ heroku addons:add heroku-postgresql
Adding heroku-postgresql on publicprivateip... done, v4 (free)
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore.
Use `heroku addons:docs heroku-postgresql` to view documentation.

And then continue to deploy the application:

$ git push heroku master
Fetching repository, done.
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 471 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)

-----> Ruby app detected

-----> Compressing... done, 13.7MB
-----> Launching... done, v7
       http://publicprivateip.herokuapp.com/ deployed to Heroku

To git@heroku.com:publicprivateip.git
   d128817..c713aad  master -> master

Then finally we need to create the database on Heroku:

$ heroku run rake migrate
Running `rake migrate` attached to terminal... up, run.4672
The source :rubygems is deprecated because HTTP requests are insecure.
Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.

Visiting the http://publicprivateip.herokuapp.com/ will give you of course an empty resultset [] as there is no data in our database.

Testing the webservice

We will use cURL to test our webservice.

GET system info (will give no entries as database is still empty):

$ curl -H "Content-Type: application/json" http://publicprivateip.herokuapp.com/ip

POST system info (will insert a record in the database):

$ curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"public":"","private":"", "systemname":"test"}' http://publicprivateip.herokuapp.com/ip

GET system info (will return the records from the database):

$ curl -H "Content-Type: application/json" http://publicprivateip.herokuapp.com/ip

Big note: we have not added any kind of validation or security to our application as this is merely for illustrative purposes how to create a REST API and deploy it on Heroku.