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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/rbbt/rest/main.rb', line 46
def self.registered(base)
base.module_eval do
register Sinatra::MultiRoute
helpers RbbtRESTHelpers
register Sinatra::RbbtAuth
helpers Sinatra::Cookies
helpers Sinatra::Streaming
add_sass_load_path Rbbt.share.views.compass.find(:lib)
set :public_folder, Rbbt.share.views.public.find
set :environment, ENV["RACK_ENV"].to_sym if ENV["RACK_ENV"]
attr_accessor :ajax, :layout, :format, :size, :update, :cache_type, :_, :profile
enable :cross_origin
set :allow_origin, :any
set :allow_methods, [:get, :post, :options]
set :allow_credentials, true
set :max_age, "1728000"
set :allow_headers, ['URI']
if production?
set :clean_trace, true
set :static_cache_control , [:public, {:max_age => 360_000}]
else
set :static_cache_control , [:public, {:max_age => 0}]
end
before do
method = request.request_method
$script_name = request.script_name = request.env["HTTP_SCRIPT_NAME"]
method_color = case method
when "GET"
:cyan
when "POST"
:yellow
end
Log.medium{ "#{Log.color method_color, method} #{Log.color(:blue, request.ip)}: " << request.path_info.gsub('/', Log.color(:blue, "/")) << ". Params: " << Log.color(:blue, Misc.fingerprint(params))}
process_common_parameters
'Access-Control-Allow-Origin' => '*'
if profile
RubyProf.start
end
end
after do
method = request.request_method
method_color = case method
when "GET"
:cyan
when "POST"
:green
end
status = response.status.to_s
case status.to_i
when 200
color = :green
when 202
color = :yellow
when 404,500
color = :red
else
color = nil
end
Log.medium{ "#{Log.color method_color, method} #{Log.color :blue, request.ip}: " << request.path_info.gsub('/', Log.color(:blue, "/")) << ". Status: " << Log.color(color, status) }
if profile
result = RubyProf.stop
printer = RubyProf::MultiPrinter.new(result)
dir = TmpFile.tmp_file
dir = settings.file_dir
FileUtils.mkdir_p dir unless File.exists? dir
printer.print(:path => dir, :profile => 'profile')
Log.info{ "Profile saved at #{ dir }: #{request.env["REQUEST_URI"]}" }
end
response.["URI"] = request.env["REQUEST_URI"]
end
add_sass_load_path Rbbt.views.compass.find
get '/robots.txt' do
send_file locate_file('public/robots.txt')
end
get '/files/:filename' do
cache_control :public, :max_age => 360000 if production?
file = settings.file_dir[params[:filename]].find
send_file file
end
get %r{/js-find/(.*)(.js)?} do
name = consume_parameter(:captures).first
script_file = locate_javascript(name)
content_type 'text/javascript', :charset => 'utf-8' if script_file =~ /\.js$/
cache_control :public, :max_age => 360000 if production?
send_file script_file
end
get %r{/css-find/(.*)(.css)?} do
name = consume_parameter(:captures).first
script_file = locate_css(name)
content_type 'text/css', :charset => 'utf-8' if script_file =~ /\.js$/
cache_control :public, :max_age => 360000 if production?
send_file script_file
end
get '/stylesheets/*' do
name = consume_parameter :splat
file = locate_sass((name*"/").sub(/.css$/,''))
content_type 'text/css', :charset => 'utf-8'
cache_control :public, :max_age => 360000 if production?
cache("css: #{File.basename(file)}", :_template_file => file, :_send_file => true, :cache_type => production? ? :synchronous : :none) do
Log.debug{ "Rendering stylesheet: #{ file }" }
render_sass(file)
end
end
get '/' do
template_render('main', params, 'main', :cache_type => :asynchronous)
end
get '/main/*' do |file|
template = File.join 'main', file
name = template.gsub(/\//,' - ')
template_render(template, params, name, :cache_type => :asynchronous)
end
get '/help/?:section?' do
if params[:section]
section = params[:section]
template_render('help/' << section, params, section, :cache_type => :asynchronous)
else
template_render('help', params, 'help', :cache_type => :asynchronous)
end
end
get '/permalink/:id' do
content_type "text/html"
layout_file = locate_template('layout')
Haml::Engine.new(Open.read(layout_file), :filename => layout_file).render(self) do
settings.permalink_dir[params[:id]].read
end
end
error TemplateMissing do |e|
halt 404, e.message
end
error RemoteServerError do |e|
raise e unless @format == :html
content_type "text"
halt 503, [e.class.to_s, e.message] * " => " << "\n\n" << (e.backtrace * "\n").gsub('`',"'")
end
error Exception do |e|
raise e unless @format == :html
content_type "text"
halt 500, [e.class.to_s, e.message] * " => " << "\n\n" << (e.backtrace * "\n").gsub('`',"'")
end
get '/fonts/*' do
filename = params[:splat].first
file = File.join(settings.public_folder, 'fonts', filename)
send_file file
end
get '/opt/*' do
filename = params[:splat].first
file = locate_file(Path.setup('public/opt')[filename])
send_file file.find
end
get '/images/*' do
filename = params[:splat].first
file = locate_file(Path.setup('public/images')[filename])
send_file file.find
end
get '/file/*' do
filename = params[:splat].first
file = locate_file(Path.setup('public')[filename])
send_file file.find
end
require 'rbbt/rest/monitor'
register Sinatra::RbbtRESTMonitor
end
end
|