-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
170 lines (133 loc) · 3.27 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require "sinatra"
require "haml"
require "rdiscount"
require "active_record"
require "logger"
Dir.glob("models/*"){|model| require_relative model}
ActiveRecord::Base.logger = Logger.new(STDOUT)
class ChordProShare < Sinatra::Base
require_relative "config/database.rb"
enable :sessions
helpers do
def authorized?
!current_user.nil?
end
def logout
session[:user] = nil
end
def current_user
User.find_by_id(session[:user])
end
def current_user=(user)
session[:user] = user.id
end
def errors
session[:errors] ||= []
end
def flush_errors
session[:errors] = []
end
def register_errors(messages)
messages = *messages
session[:errors] ||= []
session[:errors] += messages
end
end
before do
unless request.path_info =~ /^\/(login|logout|register)$/
redirect to("/#") unless authorized?
end
if request.path_info =~ /^\/(preview|download|render)$/
@doc = current_user.docs.find_by_id(params[:id]) || Doc.new(
markup: params[:markup],
name: params[:name]
)
end
flush_errors
end
get "/" do
haml :index, locals: {docs: current_user.docs}
end
get "/doc/new" do
haml :edit, locals: {doc: Doc.new}
end
get "/doc/:id" do
id = params[:id]
doc = current_user.docs.find_by_id(id)
if doc.nil?
register_errors("Could not find requested Document")
haml :index, locals: {docs: current_user.docs}
else
haml :show, locals: {doc: doc, preview: doc.render}
end
end
get "/doc/:id/edit" do
id = params[:id]
doc = current_user.docs.find_by_id(id)
if doc.nil?
register_errors("Could not find requested Document")
haml :index, locals: {docs: current_user.docs}
else
haml :edit, locals: {doc: doc}
end
end
post "/doc" do
id = params[:id]
name = params[:name]
markup = params[:markup]
doc = current_user.docs.find_by_id(id) || current_user.docs.new
doc.name = name
doc.markup = markup
if doc.save
redirect to("/doc/#{doc.id}")
else
register_errors(doc.errors.full_messages) unless doc.save
haml :edit, locals: {doc: doc}
end
end
post "/preview" do
@doc.render
end
post "/download" do
txt = ChordProTXT.new(@doc)
send_file(txt.path, filename: txt.name, type: "text/plain")
end
post "/render" do
pdf = ChordProPDF.new(@doc)
send_file(pdf.path, filename: pdf.name, type: "application/pdf")
end
get "/#" do
haml :login
end
post "/#" do
user = User.find_by_email(params[:email])
if !user.nil? && user.valid_password?(params[:password])
self.current_user = user
redirect to("/")
else
register_errors("Invalid Email/Password")
haml :login
end
end
get "/logout" do
logout
redirect to("/")
end
get "/register" do
haml :register
end
post "/register" do
user = User.new(
email: params[:email],
password: params[:password],
password_confirmation: params[:password_confirmation]
)
if user.save
self.current_user = user
redirect to("/")
else
register_errors(user.errors)
haml :register
end
end
end