2014-11-11

How to start Sinatra, DataMapper with PostgreSQL project on ArchLinux

To start a new project using Sinatra, DataMapper and PostgreSQL, first thing to do is installing Ruby and PostgreSQL:

pacman -S ruby postgresql postgresql-libs

then you'll need to install the required libraries using gem command:

gem install \
sinatra \
data_mapper \
puma \
dm-postgres-adapter \

bundle \

rerun \
redis-rack \

awesome_print \
pry \
dm-aggregates \
better_errors \
dm-transactions \

dm-timestamps \
sinatra-contrib \
binding_of_caller \
creek \
dm-noisy-failures \
roman-numerals \

Of course, not all of them required, you'll only need first 4 gems. Puma is the fastest and the easiest Ruby web app server to setup IMHO.

First you'll need to craete the database, using this command:

# create a database location
sudo su - postgres <<EOF
initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
EOF

# start the server 
sudo systemctl enable postgresql
sudo systemctl start postgresql

# create user, database and set the privilege
sudo su - postgres <<EOF
createuser myuser
createdb mydb
psql -c 'GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;'
EOF

Then you'll need to create a ruby source file, for example appserver.rb:

require 'sinatra'
require 'data_mapper'

# show SQL queries on console
#DataMapper::Logger.new($stderr, :debug) 
# create exceptions when model's action failed
#DataMapper::Model.raise_on_save_failure = true 

# setup connection
DataMapper.setup(:default, 'postgres://myuser@127.0.0.1/mydb')

# create a model
class User
  include DataMapper::Resource
  property :id, Serial
  property :name, String
end

# initialize models
DataMapper.finalize

# visit this url for the first time
get '/database/reset' do
  DataMapper.auto_migrate!
  return 'database reset completed'
end

# visit http://localhost:4567/ to show all user
get '/' do 
  us = User.all
  return 'no user, visit /user/add/(name) to add a user' if us.length == 0
  html = '<ul>'
  us.each do |u|
    html << "<li>#{u.id}: #{u.name} <a href='/user/del/#{u.id}'>[del]</a></li>"
  end
  html << '</ul>'
  return html
end

# visit http://localhost:4567/user/add/[name] to add a user
get '/user/add/:name' do 
  u = User.new name: params[:name]
  return 'failed to create user' unless u.save
  return "user created with id=#{u.id}" 
end

# visit http://localhost:4567/user/delete/[id] to delete a user
get '/user/del/:id' do 
  id = params[:id].to_i
  u = User.get id
  return 'user does not exists' if u.nil?
  return 'failed to delete this user' unless u.destroy
  return "user #{u.name} deleted"
end

Start the server using this command:

ruby appserver.rb -s Puma

After that, just visit http://localhost:4567/database/reset to create tables based on models.
Then just visit any link above to test it. Of course this just the basic, you could use erb rather than generate html using strings, with combination of JSON format and doT, just let the client's computer that generate the UI.

No comments :

Post a Comment

THINK: is it True? is it Helpful? is it Inspiring? is it Necessary? is it Kind?