Skip to content

Commit

Permalink
Enhancements to lolsrv and more resilient when using Github API
Browse files Browse the repository at this point in the history
Add shotgun and Guard for faster, better, stronger development
Consume NEW url and timestamp fields from plugin lolcommits/lolcommits#181
Rely on timestamp data when Github API call is not successful
Github API auth
Turned on Sintra config magic
Switched to modular Sintra for less config loading overhead
Google API keys read from environment, when available (reads from heroku when deployed)

Style updates and JS optimizations via lazy loading
Specify directly what size the images are
  • Loading branch information
drewwells committed Dec 6, 2013
1 parent 01ae87a commit d07072a
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.db
public/img/commits
config.yml
output.log
9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
source "http://rubygems.org"
ruby "2.0.0"

gem "sinatra"
gem "sinatra-contrib"
gem "octokit"
gem "mongo"
gem "bson"
gem "bson_ext"
gem "json"
gem "emk-sinatra-url-for", require: "sinatra/url_for"
gem "emk-sinatra-url-for", require: "sinatra/url_for"

group :development do
gem "shotgun"
gem "guard-rack"
end
52 changes: 52 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,44 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.2.8)
backports (3.3.5)
bson (1.8.0)
activesupport
bson_ext (1.8.0)
bson (~> 1.8.0)
coderay (1.1.0)
emk-sinatra-url-for (0.2.1)
sinatra (>= 0.9.1.1)
eventmachine (1.0.3)
faraday (0.8.4)
multipart-post (~> 1.1)
faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
ffi (1.3.1)
formatador (0.2.4)
guard (1.8.3)
formatador (>= 0.2.4)
listen (~> 1.3)
lumberjack (>= 1.0.2)
pry (>= 0.9.10)
thor (>= 0.14.6)
guard-rack (1.3.1)
ffi (~> 1.3.1)
guard (~> 1.1)
libnotify (~> 0.1.3)
rb-inotify (>= 0.5.1)
spoon (~> 0.0.1)
hashie (1.2.0)
i18n (0.6.1)
json (1.7.5)
libnotify (0.1.4)
ffi (>= 0.6.2)
listen (1.3.1)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
rb-kqueue (>= 0.2)
lumberjack (1.0.4)
method_source (0.8.2)
mongo (1.8.0)
bson (~> 1.8.0)
multi_json (1.5.0)
Expand All @@ -30,13 +55,37 @@ GEM
hashie (~> 1.2)
multi_json (~> 1.3)
netrc (~> 0.7.7)
pry (0.9.12.4)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
rack (1.4.1)
rack-protection (1.3.2)
rack
rack-test (0.6.2)
rack (>= 1.0)
rb-fsevent (0.9.3)
rb-inotify (0.9.2)
ffi (>= 0.5.0)
rb-kqueue (0.2.0)
ffi (>= 0.5.0)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
sinatra-contrib (1.3.2)
backports (>= 2.0)
eventmachine
rack-protection
rack-test
sinatra (~> 1.3.0)
tilt (~> 1.3)
slop (3.4.7)
spoon (0.0.4)
ffi
thor (0.18.1)
tilt (1.3.3)

PLATFORMS
Expand All @@ -46,7 +95,10 @@ DEPENDENCIES
bson
bson_ext
emk-sinatra-url-for
guard-rack
json
mongo
octokit
shotgun
sinatra
sinatra-contrib
4 changes: 3 additions & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
github_repo: "eteubert/show-off-lolcommits"
development:
CLIENT_ID: "your client id"
CLIENT_SECRET: "your client secret"
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require './index.rb'
run Sinatra::Application
run LolServer
124 changes: 77 additions & 47 deletions index.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,104 @@
require "bundler"
Bundler.require

require "sinatra"
require "sinatra/config_file"
require "pp"
require "uri"
require "yaml"
require "logger"
require 'fileutils'

mongo_db = nil
class LolServer < Sinatra::Base
helpers Sinatra::UrlForHelper
register Sinatra::ConfigFile
config_file 'config.yml'

def get_connection
return @db_connection if @db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
@db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
@db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
@db_connection
end
def self.get_connection
return @db_connection if @db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
@db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
@db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
@db_connection
end

configure :development do
mongo_db = Mongo::MongoClient.new('localhost', 27017).db('lolcommits')
end
configure do
$log = Logger.new('output.log','weekly')

configure :production do
mongo_db = get_connection
end
set :logging, false

mongo_db = nil
dirname = File.dirname('logs')
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end

configure :development do
mongo_db = Mongo::MongoClient.new('localhost', 27017).db('lolcommits')
ENV['CLIENT_ID'] = settings.CLIENT_ID
ENV['CLIENT_SECRET'] = settings.CLIENT_SECRET
$log.level = Logger::DEBUG
end

mongo_grid = Mongo::Grid.new(mongo_db)
mongo_commits = mongo_db['commit']
config = YAML.load(File.open('config.yml'))
configure :production do
mongo_db = self::get_connection
$log.level = Logger::WARN
end

get '/' do
@repo = config['github_repo']
set :mongo_grid => Mongo::Grid.new(mongo_db)
set :mongo_commits => mongo_db['commit']
end

mongo_commits.find().each { |commit|
if !commit['date']
begin
puts "x"
if github_commit = Octokit.commit(@repo, commit['sha'])
date = DateTime.strptime(github_commit['commit']['author']['date']).to_time.to_i
mongo_commits.update({"_id" => commit["_id"]}, {"$set" => { "date" => date }})
end
rescue Exception => e
# pp e
# raise
end
get '/' do
client = Octokit::Client.new \
:client_id => ENV['CLIENT_ID'],
:client_secret => ENV['CLIENT_SECRET']

settings.mongo_commits.find({ 'date' => {'$exists' => false} }).each { |commit|
begin
#This is great and all, but it needs to be authenticated to use
github_commit = Octokit.commit(commit['repo'], commit['sha'])
if github_commit
p 'Found Commit'
@repo = commit['repo']
date = DateTime.strptime(github_commit['commit']['author']['date']).to_time.to_i
settings.mongo_commits.update({"_id" => commit["_id"]}, {"$set" => { "date" => date }})
else
settings.mongo_commits.update({"_id" => commit["_id"]}, {"$set" => { "date" => commit["time"] }})
end
rescue Exception => e
pp e
#raise
end
}

@commits = mongo_commits.find().sort({date: -1})
@commits = settings.mongo_commits.find().sort({date: -1})
erb :index
end
end

get '/lol/:sha' do |sha|
get '/lol/:sha' do |sha|
content_type 'image/jpg'
begin
file = mongo_grid.get mongo_commits.find(sha: sha).first['image_id']
file = settings.mongo_grid.get settings.mongo_commits.find(sha: sha).first['image_id']
response['Cache-Control'] = "public, max-age=60"
file.read
rescue Exception => e
pp e
raise Sinatra::NotFound
end
end
end

get '/lols' do
commits = mongo_commits.find()
commits.to_a.map { |c| {sha: c['sha']} }.to_json
end
get '/lols' do
commits = settings.mongo_commits.find().sort({date: -1})
commits.to_a.map { |c| {sha: c['sha']} }.to_json
end

post '/uplol' do
mongo_commits.insert(
post '/uplol' do
settings.mongo_commits.insert(
sha: params['sha'],
image_id: mongo_grid.put(params['lol'][:tempfile])
url: params['url'],
time: params['date'],
repo: params['repo'],
image_id: settings.mongo_grid.put(params['lol'][:tempfile])
)
end
end
end
3 changes: 3 additions & 0 deletions public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ body {
-moz-box-shadow: 0px 1px 3px rgba(0,0,0,0.3);
-webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.3);
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.3);
display: inline-block;
height: 225px;
width: 300px;
}


Expand Down
30 changes: 30 additions & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
var images = $.makeArray(document.querySelectorAll('.lolimage'));
var pos = -301;
var lazy = function() {
var indexes = [],
h = window.innerHeight + 100,
y = window.scrollY;

if(y - pos < 300 && !images.length) {
return;
}
pos = y;

images.forEach(function(image, i) {
var top = image.getBoundingClientRect().top;
if(top<h) {
indexes.push(i);
}
});

indexes.sort(function(a, b){
return b > a;
}).forEach(function(n){
images[n].src = images[n].getAttribute('data-src');
console.log(n);
images.splice(n,1);
});
};
$(function(){
lazy();
$(window).scroll(lazy);
});
4 changes: 2 additions & 2 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<div class="row">
<% @commits.each do |commit| %>
<div class="span4" style="margin-bottom: 20px">
<a href="https://github.com/<%= @repo %>/commit/<%= commit['sha'] %>" target="_blank">
<img class="lolimage" src="<%= url_for "/lol/#{commit['sha']}" %>" width="640" height="480" title="<%= DateTime.strptime(commit['date'].to_s, "%s").strftime("%Y-%m-%d %H:%M:%S") if commit['date'] %>"/>
<a href="<%= commit['url'] %>" target="_blank">
<img class="lolimage" src="" data-src="<%= url_for "/lol/#{commit['sha']}" %>" width="640" height="480" title="<%= DateTime.strptime(commit['date'].to_s, "%s").strftime("%Y-%m-%d %H:%M:%S") if commit['date'] %>"/>
</a>
</div>
<% end %>
Expand Down

0 comments on commit d07072a

Please sign in to comment.