-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
99 lines (83 loc) · 2.04 KB
/
app.rb
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
97
98
99
require 'sinatra'
require 'active_support/deprecation'
require 'sinatra_more/markup_plugin'
require 'sinatra_more/render_plugin'
require 'data_mapper'
require 'fog'
require 'unf'
require 'net/ssh'
require 'json'
require 'omniauth'
require 'omniauth-github'
require 'sidekiq'
require 'sidekiq/web'
require 'digest/md5'
require './env' if File.exists?('env.rb')
require_relative 'lib/backup'
require_relative 'lib/user'
require_relative 'lib/storage'
require_relative 'lib/workers/backup_worker'
register SinatraMore::MarkupPlugin
register SinatraMore::RenderPlugin
configure do
enable :sessions
set :session_secret, ENV['SESSION_SECRET']
end
STORAGE = Storage.connect unless Sinatra::Base.test?
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/data/#{Sinatra::Base.environment}.db")
DataMapper.finalize.auto_upgrade!
use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
end
get '/auth/:provider/callback' do
user = User.create_from_omniauth(request.env["omniauth.auth"])
session[:user_id] = user.id
redirect '/'
end
get '/logout' do
session[:user_id] = nil
redirect '/'
end
get '/' do
current_user ? erb(:profile) : erb(:index)
end
post '/backups' do
halt 401, 'Not authorized!' unless current_user
Backup.create(params[:backup], current_user)
redirect '/'
end
get '/backups/:id/edit' do
perform_action do |backup|
erb :"backups/edit"
end
end
put '/backups/:id' do
perform_action do |backup|
redirect '/' if backup.update(params[:backup])
end
end
delete '/backups/:id' do
perform_action do |backup|
backup.destroy_with_directory
redirect '/'
end
end
post '/backups/:id/perform' do
perform_action do |backup|
backup.perform_async
redirect '/'
end
end
def perform_action &block
@backup = Backup.get(params[:id])
if current_user && @backup.owner?(current_user)
yield @backup if block_given?
else
halt 401, 'Not authorized!'
end
end
helpers do
def current_user
@current_user ||= User.get(session[:user_id]) if session[:user_id]
end
end