Permalink
Browse files

Switch mongodb+mongoid

  • Loading branch information...
1 parent 05c778c commit 1ef385ab810c4779c6de894e7bd01cdff5b359c3 @sickill committed Jan 15, 2011
Showing with 110 additions and 15 deletions.
  1. +1 −0 .gitignore
  2. +5 −0 Gemfile
  3. +34 −0 Gemfile.lock
  4. +4 −3 config.ru
  5. +23 −0 config/mongoid.yml
  6. +22 −12 sickounter.rb → lib/app.rb
  7. +21 −0 lib/models.rb
View
@@ -1 +1,2 @@
+.bundle
counters/*
View
@@ -0,0 +1,5 @@
+source "http://rubygems.org"
+
+gem "sinatra"
+gem "mongoid", "2.0.0.rc.4"
+gem "bson_ext", "1.1.5"
View
@@ -0,0 +1,34 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ i18n (~> 0.4)
+ activesupport (3.0.3)
+ bson (1.1.5)
+ bson_ext (1.1.5)
+ builder (2.1.2)
+ i18n (0.5.0)
+ mongo (1.1.5)
+ bson (>= 1.1.5)
+ mongoid (2.0.0.rc.4)
+ activemodel (~> 3.0)
+ mongo (~> 1.1.5)
+ tzinfo (~> 0.3.22)
+ will_paginate (~> 3.0.pre)
+ rack (1.2.1)
+ sinatra (1.1.2)
+ rack (~> 1.1)
+ tilt (~> 1.2)
+ tilt (1.2.1)
+ tzinfo (0.3.24)
+ will_paginate (3.0.pre2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bson_ext (= 1.1.5)
+ mongoid (= 2.0.0.rc.4)
+ sinatra
View
@@ -1,5 +1,6 @@
-require 'rubygems'
-require 'sickounter'
+require "bundler"
+Bundler.setup
-run Sinatra::Application
+require File.join(File.dirname(__FILE__), "lib", "app")
+run Sinatra::Application
View
@@ -0,0 +1,23 @@
+defaults: &defaults
+ autocreate_indexes: false
+ allow_dynamic_fields: true
+ include_root_in_json: false
+ parameterize_keys: true
+ persist_in_safe_mode: false
+ raise_not_found_error: true
+ reconnect_time: 3
+
+development:
+ <<: *defaults
+ host: localhost
+ database: sickounter_development
+
+test:
+ <<: *defaults
+ host: localhost
+ database: sickounter_test
+
+# set these environment variables on your prod server
+production:
+ <<: *defaults
+ uri: <%= ENV['MONGOHQ_URL'] %>
@@ -1,18 +1,28 @@
-require 'sinatra'
-
-COUNTERS_DIR = ENV['COUNTERS_DIR'] || File.join(File.dirname(__FILE__), 'counters')
+require "sinatra"
+require "uri"
+require_relative "models"
get '/' do
- url = params[:f] or raise Sinatra::NotFound
- filename = url.gsub(/https?:\/\//, "").gsub(/[^a-zA-Z0-9_-]/, ".") + ".txt"
- path = File.join(COUNTERS_DIR, filename)
- val = (File.read(path).strip.to_i rescue 0) + 1
- File.open(path, "w") do |f|
- f.flock(File::LOCK_EX)
- f.puts(val.to_s)
- f.flock(File::LOCK_UN)
+ f = params[:f] or raise Sinatra::NotFound
+
+ target_url = URI.parse(f)
+ referer_url = URI.parse(request.referer)
+
+ if host = target_url.host || referer_url.host
+ domain = Domain.find_or_create_by(:name => host)
+
+ if target_url.path[0] == "/"
+ path = target_url.path
+ else
+ path = File.dirname(referer_url.path + " ") + "/" + target_url.path
+ end
+
+ download = domain.downloads.find_or_create_by(:path => path)
+ download.count += 1
+ download.save
end
- redirect(url)
+
+ redirect(f)
end
get '/stats' do
View
@@ -0,0 +1,21 @@
+require "mongoid"
+
+Mongoid.configure do |config|
+ file_name = File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
+ settings = YAML.load(ERB.new(File.new(file_name).read).result)
+
+ config.from_hash(settings[ENV['RACK_ENV']])
+end
+
+class Domain
+ include Mongoid::Document
+ field :name
+ embeds_many :downloads
+end
+
+class Download
+ include Mongoid::Document
+ field :path
+ field :count, :type => Integer, :default => 0
+ embedded_in :domain, :inverse_of => :downloads
+end

0 comments on commit 1ef385a

Please sign in to comment.