Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created November 29, 2012 19:12
Show Gist options
  • Save sycobuny/4171191 to your computer and use it in GitHub Desktop.
Save sycobuny/4171191 to your computer and use it in GitHub Desktop.
A bare-bones "just getting started" sinatra+sequel script
#!/usr/bin/env rackup
# this sets up Bundler and has it require all the gems you mention in your Gemfile.
# neat!
require 'bundler'
# I've never investigated what these individually are doing, save that you can have
# different dev and production gemsets. I never do, but I keep the code the way it
# is.
Bundler.setup :default, ENV['RACK_ENV'].to_sym
Bundler.require :default, ENV['RACK_ENV'].to_sym
# change the load path to include the current dir, as ruby 1.9 stripped that out
$LOAD_PATH.unshift File.expand_path('.', File.dirname(__FILE__))
# load our primary web application
require 'app'
# run the application
run Sinatra::Application
# NB: these are now taken care of by config.ru
# require 'sinatra'
# require 'sequel'
DB = Sequel.connect(ENV['DATABASE_URL'])
get '/' do
"Hello, world!"
end
get '/some_db_stuff' do
# SELECT * FROM "accounts" WHERE "admin" IS TRUE LIMIT 1;
account = DB[:accounts].filter do
{:admin => true}
end.first
"The account name is #{account[:name]}"
end
__END__
The environment variable DATABASE_URL should be formatted like a URL:
postgres://username:[email protected]:port/dbname
You'll want to make a table named accounts with some data:
CREATE TABLE accounts (id SERIAL PRIMARY KEY, name TEXT NOT NULL, admin BOOLEAN NOT NULL DEFAULT FALSE);
INSERT INTO accounts (name, admin) VALUES
('steve', FALSE),
('matt', TRUE),
('justin', FALSE);
To start this server:
DATABASE_URL="url_from_earlier" rackup
`rackup` starts whatever rack server you're using by default on port 9292, so navigate to the following URLs while the server is running:
http://localhost:9292/
http://localhost:9292/some_db_stuff
source :rubygems
gem 'sinatra'
gem 'sequel'
gem 'pg'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment