Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Update Ruby/Gems and move static file serving to Rack #20

Merged
merged 4 commits into from

3 participants

@carlosdp

This PR is what is in production right now. Upgraded gems and Ruby to 2.0. Also moved static file serving to Rack pushing that load to the server itself. This should fix the gradual slow down that seemed to be resulting from static files somehow.

Also, changed DB configuration to only load connection after first use. This makes local development easier if you don't have PG or Redis installed.

carlosdp added some commits
@carlosdp carlosdp Refactor DB to initiate connections on first query
Now the DB interfaces will only load a connection if they are actually
used a first time. This also allows someone who does not have PG or
Redis installed to run hurl.
6211697
@carlosdp carlosdp Update gems to latest versions ab8030f
@carlosdp carlosdp Serve static files via Rack
Move static file serving directly to Rack. This passes off the load of
static file resolution directly to the server.
8f5fb53
@carlosdp carlosdp Use ruby 2.0 83fde79
@kevinburke

awesome. lgtm

@dougblack
Owner

:+1:

@kevinburke kevinburke merged commit a69d8e1 into twilio:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2013
  1. @carlosdp

    Refactor DB to initiate connections on first query

    carlosdp authored
    Now the DB interfaces will only load a connection if they are actually
    used a first time. This also allows someone who does not have PG or
    Redis installed to run hurl.
  2. @carlosdp
  3. @carlosdp

    Serve static files via Rack

    carlosdp authored
    Move static file serving directly to Rack. This passes off the load of
    static file resolution directly to the server.
  4. @carlosdp

    Use ruby 2.0

    carlosdp authored
This page is out of date. Refresh to see the latest.
Showing with 74 additions and 41 deletions.
  1. +1 −0  Gemfile
  2. +27 −24 Gemfile.lock
  3. +26 −17 app/models/db.rb
  4. +20 −0 config.ru
View
1  Gemfile
@@ -1,4 +1,5 @@
source 'https://rubygems.org'
+ruby '2.0.0'
gem 'sinatra', '~>1.0'
gem 'yajl-ruby', '~>1.1.0'
View
51 Gemfile.lock
@@ -1,48 +1,51 @@
GEM
remote: https://rubygems.org/
specs:
- addressable (2.3.2)
+ addressable (2.3.5)
coderay (0.8.357)
- curb (0.7.8)
- faraday (0.8.4)
- multipart-post (~> 1.1)
- faraday_middleware (0.8.8)
+ curb (0.7.18)
+ faraday (0.8.8)
+ multipart-post (~> 1.2.0)
+ faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
hashie (1.2.0)
- json (1.7.3)
- mime-types (1.16)
- multi_json (1.3.6)
- multipart-post (1.1.5)
+ json (1.8.0)
+ mime-types (1.23)
+ multi_json (1.7.7)
+ multipart-post (1.2.0)
mustache (0.11.2)
oauth2 (0.5.2)
faraday (~> 0.7)
multi_json (~> 1.0)
- octokit (1.11.0)
+ octokit (1.13.0)
addressable (~> 2.2)
faraday (~> 0.8)
faraday_middleware (~> 0.8)
hashie (~> 1.2)
multi_json (~> 1.3)
- pg (0.14.1)
- rack (1.2.1)
- redis (3.0.2)
- rest-client (1.6.1)
+ pg (0.16.0)
+ rack (1.5.2)
+ rack-protection (1.5.0)
+ rack
+ redis (3.0.4)
+ rest-client (1.6.7)
mime-types (>= 1.16)
- shotgun (0.8)
+ shotgun (0.9)
rack (>= 1.0)
- sinatra (1.1.0)
- rack (~> 1.1)
- tilt (~> 1.1)
- sinatra_auth_github (0.10.0)
+ sinatra (1.4.3)
+ rack (~> 1.4)
+ rack-protection (~> 1.4)
+ tilt (~> 1.3, >= 1.3.4)
+ sinatra_auth_github (0.10.1)
sinatra (~> 1.0)
- warden-github (~> 0.10.0)
- tilt (1.1)
- warden (1.2.1)
+ warden-github (~> 0.10.2)
+ tilt (1.4.1)
+ warden (1.2.3)
rack (>= 1.0)
- warden-github (0.10.0)
+ warden-github (0.10.2)
json (~> 1.5)
oauth2 (~> 0.5.2)
- octokit (~> 1.11.0)
+ octokit (~> 1.13.0)
rest-client (~> 1.6.1)
warden (~> 1.0)
yajl-ruby (~> 1.1)
View
43 app/models/db.rb
@@ -13,53 +13,62 @@ def self.decode(object)
end
class PostgresDB < AbstractDB
- CONN = PG::Connection::new(
- ENV.fetch("POSTGRES_HOST", "localhost"),
- ENV.fetch("POSTGRES_PORT", 5432),
- :dbname => ENV.fetch("POSTGRES_DATABASE", "hurls"),
- :user => ENV.fetch("POSTGRES_USER", "postgres"),
- :password => ENV.fetch("POSTGRES_PASSWORD", "postgres")
- )
+
+ def self.connection
+ @@connection ||= PG::Connection::new(
+ ENV.fetch("POSTGRES_HOST", "localhost"),
+ ENV.fetch("POSTGRES_PORT", 5432),
+ :dbname => ENV.fetch("POSTGRES_DATABASE", "hurls"),
+ :user => ENV.fetch("POSTGRES_USER", "postgres"),
+ :password => ENV.fetch("POSTGRES_PASSWORD", "postgres")
+ )
+ end
def self.select_query(scope)
- "SELECT content::bytea FROM %s WHERE id = $1 LIMIT 1" % CONN.escape_string(scope.to_s)
+ "SELECT content::bytea FROM %s WHERE id = $1 LIMIT 1" % connection.escape_string(scope.to_s)
end
def self.find(scope, id)
- CONN.exec(select_query(scope), [id], 1) do |result|
+ connection.exec(select_query(scope), [id], 1) do |result|
decode(result.getvalue(0, 0)) if result.num_tuples >= 1
end
end
def self.insert_query(scope)
- "INSERT INTO %s VALUES ($1::varchar, $2::bytea)" % CONN.escape_string(scope.to_s)
+ "INSERT INTO %s VALUES ($1::varchar, $2::bytea)" % connection.escape_string(scope.to_s)
end
def self.save(scope, id, content)
- CONN.exec(insert_query(scope), [id, {:value => encode(content), :format => 1}])
+ connection.exec(insert_query(scope), [id, {:value => encode(content), :format => 1}])
end
def self.count(scope)
- CONN.exec("SELECT COUNT(*) FROM %s" % CONN.escape_string(scope.to_s)) do |result|
+ connection.exec("SELECT COUNT(*) FROM %s" % connection.escape_string(scope.to_s)) do |result|
result.getvalue(0, 0)
end
end
end
class RedisDB < AbstractDB
- uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://127.0.0.1:6379"))
- CONNECTION = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
+
+ def self.connection
+ @@uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://127.0.0.1:6379"))
+ @@connection ||= Redis.new(
+ :host => @@uri.host,
+ :port => @@uri.port,
+ :password => @@uri.password)
+ end
def self.find(scope, id)
- decode(CONNECTION.get("hurl/#{scope}/#{id}"))
+ decode(connection.get("hurl/#{scope}/#{id}"))
end
def self.save(scope, id, content)
- CONNECTION.set("hurl/#{scope}/#{id}", encode(content))
+ connection.set("hurl/#{scope}/#{id}", encode(content))
end
def self.count(scope)
- CONNECTION.keys("hurl/#{scope}/*").size
+ connection.keys("hurl/#{scope}/*").size
end
end
View
20 config.ru
@@ -10,4 +10,24 @@ end
require 'app/app'
+map "/js" do
+ run Rack::Directory.new("./public/js")
+end
+
+map "/css" do
+ run Rack::Directory.new("./public/css")
+end
+
+map "/img" do
+ run Rack::Directory.new("./public/img")
+end
+
+map "/favicon.ico" do
+ run Rack::File.new("./public/favicon.ico")
+end
+
+map "/robots.txt" do
+ run Rack::File.new("./public/robots.txt")
+end
+
run Hurl::App.new
Something went wrong with that request. Please try again.