forked from eteubert/lolsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancements to lolsrv and more resilient when using Github API
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
Showing
9 changed files
with
177 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.db | ||
public/img/commits | ||
config.yml | ||
output.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
require './index.rb' | ||
run Sinatra::Application | ||
run LolServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters