-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
86 lines (68 loc) · 1.85 KB
/
main.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
# Modular App Test with Sinatra-Warden
module Poobah
class FailureApp
def call(env)
env['x-rack.flash'][:notice] = env['warden'].message unless env['x-rack.flash'].nil?
[302, {'Location' => env['warden.options'][:attempted_path], 'Content-type' => 'text/html'},'']
end
end
class Public < Sinatra::Base
configure do
config = YAML::load(File.open('config/database.yml'))
environment = settings.environment.to_s
ActiveRecord::Base.establish_connection(config[environment])
ActiveRecord::Base.logger = Logger.new($stdout)
end
set :session_secret, "public sinatra app"
get '/' do
if env['warden'].authenticated? || env['warden'].authenticated?(:admin)
haml :index
else
redirect '/login'
end
end
get '/login' do
flash[:notice] = env['x-rack.flash'][:notice] unless env['x-rack.flash'].nil?
haml :login
end
get '/logout/?' do
env['warden'].logout
redirect '/'
end
post '/login' do
env['warden'].authenticate!
redirect('/')
end
post '/unauthenticated/?' do
status 401
haml :login
end
end
class Admin < Sinatra::Base
set :session_secret, "admin sinatra app"
get '/' do
if env['warden'].authenticated?(:admin)
haml :'admin/index'
else
redirect '/admin/login'
end
end
get '/login' do
flash[:notice] = env['x-rack.flash'][:notice] unless env['x-rack.flash'].nil?
haml :'admin/login'
end
post '/login' do
env['warden'].authenticate! :scope => :admin
redirect('/admin')
end
post '/unauthenticated/?' do
status 401
haml :'admin/login'
end
end
class Special < Sinatra::Base
get '/' do
haml :'special/index'
end
end
end