Skip to content
This repository was archived by the owner on Oct 16, 2021. It is now read-only.

Commit 5456ddf

Browse files
committed
Add page cache
1 parent 0b2af30 commit 5456ddf

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ gem 'activesupport'
77
gem 'pg'
88
gem 'sequel', '~> 4.49'
99

10+
gem 'rack-cache', require: 'rack/cache'
1011
gem 'rack-headers_filter', require: 'rack/headers_filter'
1112
gem 'rack-ssl-enforcer', require: 'rack/ssl-enforcer'
1213

14+
gem 'redis-rack-cache'
15+
1316
gem 'sinatra', require: 'sinatra/base'
1417
gem 'sinatra-contrib', require: 'sinatra/contrib/all'
1518
gem 'sinatra-param', require: 'sinatra/param'

Gemfile.lock

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ GEM
5252
puma (4.0.1)
5353
nio4r (~> 2.0)
5454
rack (2.0.7)
55+
rack-cache (1.9.0)
56+
rack (>= 0.4)
5557
rack-headers_filter (0.0.1)
5658
rack-protection (2.0.5)
5759
rack
@@ -61,6 +63,12 @@ GEM
6163
rb-inotify (0.10.0)
6264
ffi (~> 1.0)
6365
redcarpet (3.5.0)
66+
redis (4.1.2)
67+
redis-rack-cache (2.1.0)
68+
rack-cache (>= 1.6, < 2)
69+
redis-store (>= 1.6, < 2)
70+
redis-store (1.6.0)
71+
redis (>= 2.2, < 5)
6472
sass (3.4.25)
6573
sequel (4.49.0)
6674
sinatra (2.0.5)
@@ -95,10 +103,12 @@ DEPENDENCIES
95103
haml
96104
pg
97105
puma
106+
rack-cache
98107
rack-headers_filter
99108
rack-ssl-enforcer
100109
rake
101110
redcarpet
111+
redis-rack-cache
102112
sequel (~> 4.49)
103113
sinatra
104114
sinatra-contrib

config.ru

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler'
24
Bundler.require
35

46
Sequel.extension :core_extensions, :migration
57
DB = Sequel.connect(ENV['DATABASE_URL'])
68
DB.extension :pg_array
79

8-
Rack::Mime::MIME_TYPES.merge!({
9-
".srt" => "text/plain",
10-
".vtt" => "text/vtt",
11-
})
12-
10+
Rack::Mime::MIME_TYPES.merge!(
11+
'.srt' => 'text/plain',
12+
'.vtt' => 'text/vtt'
13+
)
1314

1415
use Rack::SslEnforcer if ENV['RACK_ENV'] == 'production'
15-
1616
use Rack::HeadersFilter
1717
use Rack::Deflater
18-
use Rack::Static, urls: ["/css", "/images", "/js", "favicon.ico"], root: "public"
18+
use Rack::Static, urls: ['/css', '/images', '/js', 'favicon.ico'],
19+
root: 'public'
20+
21+
if ENV['REDIS']
22+
use Rack::Cache, metastore: "#{ENV['REDIS']}/0/metastore",
23+
entitystore: "#{ENV['REDIS']}/0/entitystore"
24+
end
1925

2026
require './web'
2127

web.rb

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require './lib/models/session'
24

35
class Web < Sinatra::Base
@@ -10,7 +12,7 @@ class Web < Sinatra::Base
1012

1113
helpers do
1214
def title(*args)
13-
[*args].compact.join(" - ")
15+
[*args].compact.join(' - ')
1416
end
1517

1618
def url(session)
@@ -21,10 +23,8 @@ def image_url(session)
2123
case Integer(session.year)
2224
when 2010, 2011
2325
"/images/wwdc-#{session.year}.jpg"
24-
when 2012..2018
26+
when 2012..2019
2527
"/images/wwdc-#{session.year}.png"
26-
else
27-
nil
2828
end
2929
end
3030

@@ -36,74 +36,76 @@ def video_url(session)
3636
before do
3737
@query = params[:q]
3838

39-
cache_control :public, max_age: 3600
40-
41-
headers "Content-Security-Policy" => %(
42-
default-src 'self' *.asciiwwdc.com;
43-
form-action 'self';
44-
frame-ancestors 'none';
45-
object-src 'none';
46-
base-uri 'none';
47-
).gsub("\n", ' ').squeeze(' ').strip,
48-
"Link" => %(
49-
</css/screen.css>; rel=preload; as=style
50-
).gsub("\n", ' ').squeeze(' ').strip,
51-
"Referrer-Policy" => "same-origin",
52-
"Server" => '',
53-
"Strict-Transport-Security" => "max-age=63072000; includeSubDomains; preload",
54-
"X-Content-Type-Options" => "nosniff",
55-
"X-Frame-Options" => "DENY",
56-
"X-XSS-Protection" => "1; mode=block" unless settings.development?
39+
cache_control :public, max_age: 86_400
40+
41+
unless settings.development?
42+
headers 'Content-Security-Policy' => %(
43+
default-src 'self' *.asciiwwdc.com;
44+
form-action 'self';
45+
frame-ancestors 'none';
46+
object-src 'none';
47+
base-uri 'none';
48+
).gsub("\n", ' ').squeeze(' ').strip,
49+
'Link' => %(
50+
</css/screen.css>; rel=preload; as=style
51+
).gsub("\n", ' ').squeeze(' ').strip,
52+
'Referrer-Policy' => 'same-origin',
53+
'Server' => '',
54+
'Strict-Transport-Security' => 'max-age=63072000; includeSubDomains; preload',
55+
'X-Content-Type-Options' => 'nosniff',
56+
'X-Frame-Options' => 'DENY',
57+
'X-XSS-Protection' => '1; mode=block'
58+
end
5759
end
5860

5961
error Sinatra::Param::InvalidParameterError do
60-
haml :error, :locals => { :msg => env['sinatra.error'] }
62+
haml :error, locals: { msg: env['sinatra.error'] }
6163
end
6264

6365
error 404 do
64-
haml :error, :locals => { :msg => "404 Not found"}
66+
haml :error, locals: { msg: '404 Not found' }
6567
end
6668

6769
not_found do
68-
haml :error, :locals => { :msg => "404 Not found"}
70+
haml :error, locals: { msg: '404 Not found' }
6971
end
7072

7173
get '/' do
7274
@sessions = Session.select(:title, :year, :number, :track)
73-
.order(:year, :number)
74-
.all
75-
.group_by(&:year)
75+
.order(:year, :number)
76+
.all
77+
.group_by(&:year)
7678
haml :index
7779
end
7880

7981
get '/contribute' do
8082
haml :contribute
8183
end
8284

83-
get '/:year/sessions/:number', provides: [:html, :json, :vtt, :txt] do
85+
get '/:year/sessions/:number', provides: %i[html json vtt txt] do
8486
param :year, Integer, required: true
8587
param :number, Integer, required: true
8688

8789
halt 404 unless @session = Session.first(year: params[:year], number: params[:number])
8890

89-
link video_url(@session), :rel => :alternate
91+
link video_url(@session), rel: :alternate
9092

9193
respond_to do |f|
92-
f.html {haml :session}
93-
f.json {@session.to_json}
94-
f.vtt {send_file "data/#{params[:year]}/#{params[:number]}.vtt", type: :vtt}
95-
f.txt {@session.transcript}
94+
f.html { haml :session }
95+
f.json { @session.to_json }
96+
f.vtt { send_file "data/#{params[:year]}/#{params[:number]}.vtt", type: :vtt }
97+
f.txt { @session.transcript }
9698
end
9799
end
98100

99-
get '/search', provides: [:html, :json] do
101+
get '/search', provides: %i[html json] do
100102
param :q, String, blank: false
101103
param :year, Integer, in: 2010..2018
102104

103105
@sessions = Session.search(@query, params[:year])
104106

105107
respond_to do |f|
106-
f.html {haml :search}
108+
f.html { haml :search }
107109
f.json do
108110
{
109111
query: @query,
@@ -131,5 +133,4 @@ def video_url(session)
131133
pass
132134
end
133135
end
134-
135136
end

0 commit comments

Comments
 (0)