-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
93 lines (78 loc) · 1.86 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
module SocketChat
class App < Sinatra::Base
extend Settings
configure(:development) do |c|
register Sinatra::Reloader
c.also_reload('lib/**/*.rb')
end
configure(:production) do
SocketChat.setup_airbrake unless config.airbrake_api_key.nil?
set :haml, {ugly: true}
end
configure(:production, :development) do
enable(:logging, :dump_errors)
end
configure do
set :root, APP_ROOT
set :config, SocketChat.config
set :haml, {format: :html5}
enable(:sessions)
set :session_secret, config.session_secret
use Rack::Flash
end
helpers(ApplicationHelper)
get '/' do
@messages = Message.limit(20)
haml :index
end
get '/lobby' do
@rooms = Room.accessible_by(current_user)
haml :lobby
end
get '/rooms/:room_name' do |room_name|
if @room = Room.find_by_slug(room_name)
if @room.accessible_by?(current_user)
@messages = @room.messages.limit(20)
haml :room
else
403
end
else
404
end
end
post '/user/login' do
if user = User.authenticate(params[:email], params[:password])
flash[:notice] = "Logged in successfully"
session[:user_id] = user.id.to_s
redirect '/lobby'
else
flash[:notice] = "Incorrect credentials"
redirect '/'
end
end
get '/user/new' do
haml :register
end
post '/user/new' do
@user = User.create(params)
if @user.valid?
flash[:notice] = "Registered successfully!"
session[:user_id] = @user.id.to_s
redirect '/'
else
haml :register
end
end
get '/user/logout' do
session[:user_id] = nil
redirect '/'
end
error 403 do
haml :'403'
end
not_found do
haml :'404'
end
end
end