forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.rb
514 lines (439 loc) · 15.2 KB
/
router.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
require_relative 'src/env'
require 'rack'
require 'cdo/rack/locale'
require 'sinatra/base'
require 'sinatra/verbs'
require 'cdo/sinatra'
require 'cdo/geocoder'
require 'cdo/pegasus/graphics'
require 'cdo/rack/cdo_deflater'
require 'cdo/rack/request'
require 'cdo/properties'
require 'cdo/languages'
require 'dynamic_config/page_mode'
require 'active_support'
require 'base64'
require 'cgi'
require 'json'
require 'uri'
require 'cdo/rack/upgrade_insecure_requests'
require_relative 'helper_modules/dashboard'
require 'dynamic_config/dcdo'
require 'active_support/core_ext/hash'
require 'sass'
require 'sass/plugin'
if rack_env?(:production)
require 'newrelic_rpm'
# Enable GC profiler for New Relic instrumentation.
GC::Profiler.enable
NewRelic::Agent.after_fork(force_reconnect: true)
end
require 'honeybadger'
require src_dir 'database'
require src_dir 'social_metadata'
require src_dir 'forms'
require src_dir 'curriculum_router'
def http_vary_add_type(vary, type)
types = vary.to_s.split(',').map(&:strip)
return vary if types.include?('*') || types.include?(type)
types.push(type).join(',')
end
class Documents < Sinatra::Base
def self.get_head_or_post(url, &block)
get(url, &block)
head(url, &block)
post(url, &block)
end
def self.load_config_in(dir)
path = File.join(dir, 'config.json')
return {} unless File.file?(path)
JSON.parse(IO.read(path), symbolize_names: true)
end
def self.load_configs_in(dir)
configs = {}
Dir.entries(dir).each do |site|
site_dir = File.join(dir, site)
next if site == '.' || site == '..' || !File.directory?(site_dir)
configs[site] = load_config_in(site_dir)
end
configs
end
use Rack::Locale
use Rack::CdoDeflater
use Rack::UpgradeInsecureRequests
# Use dynamic config for max_age settings, with the provided default as fallback.
def self.set_max_age(type, default)
default = 60 if rack_env? :staging
default = 0 if rack_env? :development
set "#{type}_max_age", proc {DCDO.get("pegasus_#{type}_max_age", default)}
end
ONE_HOUR = 3600
configure do
dir = pegasus_dir('sites.v3')
set :launched_at, Time.now
set :configs, load_configs_in(dir)
set :views, dir
set :image_extnames, ['.png', '.jpeg', '.jpg', '.gif']
set :exclude_extnames, ['.collate']
set_max_age :document, ONE_HOUR * 4
set_max_age :document_proxy, ONE_HOUR * 2
set_max_age :image, ONE_HOUR * 10
set_max_age :image_proxy, ONE_HOUR * 5
set_max_age :static, ONE_HOUR * 10
set_max_age :static_proxy, ONE_HOUR * 5
set :read_only, CDO.read_only
set :not_found_extnames, ['.not_found', '.404']
set :redirect_extnames, ['.redirect', '.moved', '.found', '.301', '.302']
set :template_extnames, ['.erb', '.fetch', '.haml', '.html', '.md', '.txt']
set :non_static_extnames, settings.not_found_extnames + settings.redirect_extnames + settings.template_extnames + settings.exclude_extnames
set :markdown, {autolink: true, tables: true, space_after_headers: true, fenced_code_blocks: true}
Sass::Plugin.options[:cache_location] = pegasus_dir('cache', '.sass-cache')
Sass::Plugin.options[:css_location] = pegasus_dir('cache', 'css')
Sass::Plugin.options[:template_location] = shared_dir('css')
end
before do
$log.debug request.url
Honeybadger.context({url: request.url, locale: request.locale})
uri = request.path_info.chomp('/')
redirect uri unless uri.empty? || request.path_info == uri
locale = request.locale
locale = 'it-IT' if request.site == 'italia.code.org'
locale = 'es-ES' if request.site == 'ar.code.org'
locale = 'ro-RO' if request.site == 'ro.code.org'
locale = 'pt-BR' if request.site == 'br.code.org'
I18n.locale = locale
@config = settings.configs[request.site]
@header = {}
@dirs = []
if request.site == 'hourofcode.com'
@dirs << [File.join(request.site, 'i18n')]
end
@dirs << request.site
if @config
base = @config[:base]
while base
@dirs << base
base = settings.configs[base][:base]
end
end
@locals = {header: {}}
end
# Language selection
get %r{^/lang/([^/]+)/?(.*)?$} do
lang, path = params[:captures]
pass unless DB[:cdo_languages].first(code_s: lang)
dont_cache
response.set_cookie('language_', {value: lang, domain: ".#{request.site}", path: '/', expires: Time.now + (365 * 24 * 3600)})
redirect "/#{path}"
end
# Page mode selection
get '/private/pm/*' do |page_mode|
dont_cache
response.set_cookie('pm', {value: page_mode, domain: ".#{request.site}", path: '/', expires: Time.now + (7 * 24 * 3600)})
redirect "/learn?r=#{rand(100000)}"
end
# /private (protected area)
['/private', '/private/*'].each do |uri|
get_head_or_post uri do
unless rack_env?(:development)
not_authorized! unless dashboard_user_helper
forbidden! unless dashboard_user_helper.admin?
end
pass
end
end
# Static files
get '*' do |uri|
pass unless path = resolve_static('public', uri)
cache :static
NewRelic::Agent.set_transaction_name(uri) if defined? NewRelic
send_file(path)
end
get '/style.css' do
content_type :css
css, css_last_modified = combine_css 'styles', 'styles_min'
last_modified(css_last_modified) if css_last_modified > Time.at(0)
cache :static
css
end
# rubocop:disable Lint/Eval
Dir.glob(pegasus_dir('routes/*.rb')).sort.each {|path| eval(IO.read(path), nil, path, 1)}
# rubocop:enable Lint/Eval
# Manipulated images
get '/images/*' do |path|
path = File.join('/images', path)
image_data = process_image(path, settings.image_extnames, @language, request.site)
pass if image_data.nil?
last_modified image_data[:last_modified]
content_type image_data[:content_type]
cache :image
send_file(image_data[:file]) if image_data[:file]
image_data[:content]
end
# Documents
get_head_or_post '*' do |uri|
pass unless path = resolve_document(uri)
if defined? NewRelic
transaction_name = uri
transaction_name = transaction_name.sub(request.env[:splat_path_info], '') if request.env[:splat_path_info]
NewRelic::Agent.set_transaction_name(transaction_name)
end
not_found! if settings.not_found_extnames.include?(File.extname(path))
document path
end
after do
return unless response.headers['X-Pegasus-Version'] == '3'
return unless ['', 'text/html'].include?(response.content_type.to_s.split(';', 2).first.to_s.downcase)
if params.key?('embedded') && @locals[:header]['embedded_layout']
@locals[:header]['layout'] = @locals[:header]['embedded_layout']
@locals[:header]['theme'] ||= 'none'
response.headers['X-Frame-Options'] = 'ALLOWALL'
end
if @locals[:header]['content-type']
response.headers['Content-Type'] = @locals[:header]['content-type']
end
layout = @locals[:header]['layout'] || 'default'
unless ['', 'none'].include?(layout)
template = resolve_template('layouts', settings.template_extnames, layout)
raise Exception, "'#{layout}' layout not found." unless template
body render_template(template, @locals.merge({body: body.join('')}))
end
theme = @locals[:header]['theme'] || 'default'
unless ['', 'none'].include?(theme)
template = resolve_template('themes', settings.template_extnames, theme)
raise Exception, "'#{theme}' theme not found." unless template
body render_template(template, @locals.merge({body: body.join('')}))
end
end
not_found do
status 404
path = resolve_template('views', settings.template_extnames, '/404')
document(path).tap {dont_cache}
end
helpers(Dashboard) do
def content_dir(*paths)
File.join(settings.views, *paths)
end
# Get the current dashboard user record
# @returns [Hash]
#
# TODO: Switch to using `dashboard_user_helper` everywhere and remove this
def dashboard_user
@dashboard_user ||= Dashboard.db[:users][id: dashboard_user_id]
end
# Get the current dashboard user wrapped in a helper
# @returns [Dashboard::User] or nil if not signed in
#
# TODO: When we are using this everywhere, rename to just `dashboard_user`
def dashboard_user_helper
@dashboard_user_helper ||= Dashboard::User.get(dashboard_user_id)
end
# Get the current dashboard user ID
# @returns [Integer]
def dashboard_user_id
request.user_id
end
def document(path)
content = IO.read(path)
original_line_count = content.lines.count
match = content.match(/^(?<yaml>---\s*\n.*?\n?)^(---\s*$\n?)/m)
if match
@header = @locals[:header] = YAML.load(render_(match[:yaml], '.erb'))
content = match.post_match
end
line_number_offset = content.lines.count - original_line_count
@header['social'] = social_metadata
if @header['require_https'] && rack_env == :production
headers['Vary'] = http_vary_add_type(headers['Vary'], 'X-Forwarded-Proto')
redirect request.url.sub('http://', 'https://') unless request.env['HTTP_X_FORWARDED_PROTO'] == 'https'
end
if @header['max_age']
cache_for @header['max_age']
else
cache :document
end
if request.post? && !@header['allow_post']
response.headers['Allow'] = 'GET, HEAD'
error 405
end
response.headers['X-Pegasus-Version'] = '3'
begin
render_(content, File.extname(path))
rescue Haml::Error => e
if e.backtrace.first =~ /router\.rb:/ && e.line
actual_line_number = e.line - line_number_offset + 1
e.set_backtrace e.backtrace.unshift("#{path}:#{actual_line_number}")
end
raise e
end
end
def preprocess_markdown(markdown_content)
markdown_content.gsub(/```/, "```\n")
end
def post_process_html_from_markdown(full_document)
full_document.gsub!(/<p>\[\/(.*)\]<\/p>/) do
"</div>"
end
full_document.gsub!(/<p>\[(.*)\]<\/p>/) do
value = $1
if value[0] == '#'
attribute = 'id'
value = value[1..-1]
else
attribute = 'class'
end
"<div #{attribute}='#{value}'>"
end
full_document
end
def log_drupal_link(dir, uri, path)
if dir == 'drupal.code.org'
Honeybadger.notify(
error_class: "Link to v3.sites/drupal.code.org",
error_message: "#{uri} fell through to the base config directory",
environment_name: "drupal_#{rack_env}",
context: {path: path}
)
end
end
def resolve_static(subdir, uri)
return nil if settings.non_static_extnames.include?(File.extname(uri))
@dirs.each do |dir|
path = content_dir(dir, subdir, uri)
if File.file?(path)
log_drupal_link(dir, uri, path)
return path
end
end
nil
end
def resolve_template(subdir, extnames, uri)
@dirs.each do |dir|
extnames.each do |extname|
path = content_dir(dir, subdir, "#{uri}#{extname}")
if File.file?(path)
log_drupal_link(dir, "#{uri}#{extname}", path)
return path
end
end
end
# Also look for shared items.
extnames.each do |extname|
path = content_dir('..', '..', 'shared', 'haml', "#{uri}#{extname}")
if File.file?(path)
return path
end
end
nil
end
def resolve_document(uri)
extnames = settings.non_static_extnames
path = resolve_template('public', extnames, uri)
return path if path
path = resolve_template('public', extnames, File.join(uri, 'index'))
return path if path
at = uri
while at != '/'
parent = File.dirname(at)
path = resolve_template('public', extnames, File.join(parent, 'splat'))
if path
request.env[:splat_path_info] = uri[parent.length..-1]
return path
end
at = parent
end
nil
end
def resolve_image(uri)
settings.image_extnames.each do |extname|
file_path = resolve_static('public', "#{uri}#{extname}")
return file_path if file_path
end
nil
end
def render_template(path, locals={})
render_(IO.read(path), File.extname(path), locals)
rescue Haml::Error => e
if e.backtrace.first =~ /router\.rb:/
e.set_backtrace e.backtrace.unshift("#{path}:#{e.line}")
end
raise e
rescue => e
Honeybadger.context({path: path, e: e})
raise "Error rendering #{path}: #{e}"
end
def render_(body, extname, locals={})
locals = @locals.merge(locals).symbolize_keys
case extname
when '.erb', '.html'
erb body, locals: locals
when '.haml'
haml body, locals: locals
when '.fetch'
url = erb(body, locals: locals)
cache_file = cache_dir('fetch', request.site, request.path_info)
unless File.file?(cache_file) && File.mtime(cache_file) > settings.launched_at
FileUtils.mkdir_p File.dirname(cache_file)
IO.binwrite(cache_file, Net::HTTP.get(URI(url)))
end
pass unless File.file?(cache_file)
cache :static
send_file(cache_file)
when '.md', '.txt'
preprocessed = erb body, locals: locals
preprocessed = preprocess_markdown preprocessed
html = markdown preprocessed, locals: locals
post_process_html_from_markdown html
when '.redirect', '.moved', '.301'
redirect erb(body, locals: locals), 301
when '.found', '.302'
redirect erb(body, locals: locals), 302
else
raise "'#{extname}' isn't supported."
end
end
def social_metadata
metadata =
if request.site == 'csedweek.org'
{'og:site_name' => 'CSEd Week'}
else
{'og:site_name' => 'Code.org'}
end
# Metatags common to all sites.
metadata['og:title'] = @header['title'] unless @header['title'].nil_or_empty?
metadata['og:description'] = @header['description'] unless @header['description'].nil_or_empty?
metadata['fb:app_id'] = '500177453358606'
metadata['og:type'] = 'article'
metadata['article:publisher'] = 'https://www.facebook.com/Code.org'
metadata['og:url'] = request.url
(@header['social'] || {}).each_pair do |key, value|
if value == ""
metadata.delete(key)
else
metadata[key] = value
end
end
# A few pages have specific metadata defined here.
metadata.merge! get_social_metadata_for_page(request)
if request.site != 'csedweek.org'
unless metadata['og:image']
metadata['og:image'] = CDO.code_org_url('/images/default-og-image.png', 'https:')
metadata['og:image:width'] = 1220
metadata['og:image:height'] = 640
end
unless metadata['twitter:image:src']
metadata['twitter:image:src'] = CDO.code_org_url('/images/default-og-image.png', 'https:')
end
end
metadata
end
def view(uri, locals={})
path = resolve_template('views', settings.template_extnames, uri.to_s)
raise "View '#{uri}' not found." unless path
render_template(path, locals)
end
# Load helpers
load pegasus_dir('helpers.rb')
end
use CurriculumRouter
end