-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.rb
More file actions
96 lines (80 loc) · 2.02 KB
/
app.rb
File metadata and controls
96 lines (80 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$: << File.join(File.dirname(__FILE__), 'lib')
require 'rubygems'
require 'sinatra'
require 'omniauth'
require 'omniauth-github'
require 'erb'
require 'json'
require 'fileutils'
require 'database'
require 'repo'
require 'mapping'
require 'user'
airbrake_config = File.join('config', 'airbrake.json')
if File.exist?(airbrake_config)
require 'airbrake'
airbrake_config = File.open(airbrake_config) {|f| JSON.parse(f.read) }
Airbrake.configure {|config| config.api_key = airbrake_config['apikey'] }
use Airbrake::Rack
enable :raise_errors
end
use Rack::Session::Cookie
use OmniAuth::Builder do
github_config = File.join('config', 'github.json')
next unless File.exist?(github_config)
github_config = File.open(github_config) {|f| JSON.parse(f.read) }
provider :github, github_config["client_id"], github_config["secret"]
end
def ensure_user
return true if session[:user]
@body = erb :splash
halt(erb :master)
end
def current_user
session[:user]
end
get '/' do
ensure_user
@mapping = Mapping.new(current_user.nickname)
if params[:domain]
@mapping.domain = params[:domain].to_s.downcase
if @mapping.save
Repo.new({
"repository" => {
"name" => "#{current_user.nickname}.ruhoh.com",
"owner" => {
"name" => current_user.nickname
}
}
}).try_deploy
end
end
@current_user = current_user
full_name = "#{current_user.nickname}/#{current_user.nickname}.ruhoh.com"
@repos = [{
"html_url" => "http://github.com/#{full_name}",
"full_name" => full_name
}]
@body = erb :home
erb :master
end
post '/' do
payload = JSON.parse(params['payload'])
repo = Repo.new(payload)
repo.try_deploy
end
# Support both GET and POST for callbacks
%w(get post).each do |method|
send(method, "/auth/:provider/callback") do
session[:user] = User.new(env['omniauth.auth']) # => OmniAuth::AuthHash
redirect '/'
end
end
get '/auth/failure' do
puts params[:message]
redirect '/'
end
get '/logout' do
session.clear
redirect '/'
end