Skip to content

Commit 3a5304f

Browse files
committed
Optimization StoriesController#index
1 parent 484f98b commit 3a5304f

File tree

10 files changed

+176
-11
lines changed

10 files changed

+176
-11
lines changed

.dev_to/compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ x-backend: &backend
4545
PSQL_HISTFILE: /usr/local/hist/.psql_history
4646
IRB_HISTFILE: /usr/local/hist/.irb_history
4747
EDITOR: vi
48+
SKYLIGHT_AUTHENTICATION: $SKYLIGHT_AUTHENTICATION
4849
depends_on: &backend_depends_on
4950
postgres:
5051
condition: service_healthy

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ gem "sdoc", "~> 1.0", group: :doc
8989
gem "serviceworker-rails", "~> 0.5"
9090
gem "share_meow_client", "~> 0.1"
9191
gem "sitemap_generator", "~> 6.0"
92-
gem "skylight", "~> 3.1"
92+
gem "skylight"
9393
gem "slack-notifier", "~> 2.3"
9494
gem "sprockets", "~> 3.7"
9595
gem "staccato", "~> 0.5"
@@ -102,6 +102,7 @@ gem "uglifier", "~> 4.1"
102102
gem "validate_url", "~> 1.0"
103103
gem "webpacker", "~> 3.5"
104104
gem "webpush", "~> 0.3"
105+
gem "rack-mini-profiler"
105106

106107
group :development do
107108
gem "better_errors", "~> 2.5"

Gemfile.lock

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ GEM
197197
coffee-script-source
198198
execjs
199199
coffee-script-source (1.12.2)
200-
concurrent-ruby (1.1.5)
200+
concurrent-ruby (1.2.3)
201201
connection_pool (2.2.2)
202202
counter_culture (2.1.2)
203203
activerecord (>= 3.0.0)
@@ -523,7 +523,7 @@ GEM
523523
mime-types (~> 3.0)
524524
multi_xml (>= 0.5.2)
525525
httpclient (2.8.3)
526-
i18n (1.6.0)
526+
i18n (1.14.5)
527527
concurrent-ruby (~> 1.0)
528528
ice_nine (0.11.2)
529529
inflecto (0.0.2)
@@ -585,7 +585,7 @@ GEM
585585
mime-types-data (3.2018.0812)
586586
mini_mime (1.0.1)
587587
mini_portile2 (2.4.0)
588-
minitest (5.11.3)
588+
minitest (5.23.1)
589589
momentjs-rails (2.20.1)
590590
railties (>= 3.1)
591591
msgpack (1.2.4)
@@ -664,6 +664,8 @@ GEM
664664
rack (2.0.6)
665665
rack-host-redirect (1.3.0)
666666
rack
667+
rack-mini-profiler (3.1.1)
668+
rack (>= 1.2.0)
667669
rack-protection (2.0.4)
668670
rack
669671
rack-proxy (0.6.5)
@@ -830,9 +832,9 @@ GEM
830832
tilt (~> 2.0)
831833
sitemap_generator (6.0.2)
832834
builder (~> 3.0)
833-
skylight (3.1.4)
834-
skylight-core (= 3.1.4)
835-
skylight-core (3.1.4)
835+
skylight (4.3.2)
836+
skylight-core (= 4.3.2)
837+
skylight-core (4.3.2)
836838
activesupport (>= 4.2.0)
837839
slack-notifier (2.3.2)
838840
smart_properties (1.13.1)
@@ -878,7 +880,7 @@ GEM
878880
multipart-post (~> 2.0)
879881
naught (~> 1.0)
880882
simple_oauth (~> 0.3.0)
881-
tzinfo (1.2.5)
883+
tzinfo (1.2.11)
882884
thread_safe (~> 0.1)
883885
uber (0.1.0)
884886
uglifier (4.1.20)
@@ -1012,6 +1014,7 @@ DEPENDENCIES
10121014
pusher (~> 1.3)
10131015
pusher-push-notifications (~> 1.0)
10141016
rack-host-redirect (~> 1.3)
1017+
rack-mini-profiler
10151018
rack-timeout (~> 0.5)
10161019
rails (~> 5.1.6)
10171020
rails-assets-airbrake-js-client (~> 1.5)!
@@ -1040,7 +1043,7 @@ DEPENDENCIES
10401043
simplecov (~> 0.16)
10411044
sinatra (~> 2.0)
10421045
sitemap_generator (~> 6.0)
1043-
skylight (~> 3.1)
1046+
skylight
10441047
slack-notifier (~> 2.3)
10451048
sprockets (~> 3.7)
10461049
staccato (~> 0.5)

app/controllers/application_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class ApplicationController < ActionController::Base
44
include Pundit
55
include Instrumentation
66

7+
before_action :authorize_rmp, if: :authorize_rmp?
8+
79
def require_http_auth
810
authenticate_or_request_with_http_basic do |username, password|
911
username == ApplicationConfig["APP_NAME"] && password == ApplicationConfig["APP_PASSWORD"]
@@ -78,4 +80,14 @@ def append_info_to_payload(payload)
7880
super(payload)
7981
append_to_honeycomb(request, self.class.name)
8082
end
83+
84+
def authorize_rmp
85+
Rack::MiniProfiler.authorize_request
86+
end
87+
88+
def authorize_rmp?
89+
# return true unless Rails.env.production?
90+
91+
params[:rmp_token] == 'secret'
92+
end
8193
end

app/views/stories/_main_stories_feed.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
<% if !user_signed_in? && i == 4 %>
5656
<%= render "stories/sign_in_invitation" %>
5757
<% end %>
58-
<%= render "articles/single_story", story: story %>
58+
<% cache(story) do %>
59+
<%= render "articles/single_story", story: story %>
60+
<% end %>
5961
<% end %>
6062
<% end %>
6163
<% if @stories.size > 1 %>

config/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Application < Rails::Application
2222
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
2323
# config.i18n.default_locale = :de
2424

25+
config.skylight.environments << "development"
26+
config.skylight.environments << "local_production"
27+
2528
config.autoload_paths += Dir["#{config.root}/app/labor/"]
2629
config.autoload_paths += Dir["#{config.root}/app/decorators/"]
2730
config.autoload_paths += Dir["#{config.root}/app/services/"]
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# rubocop:disable Metrics/BlockLength
2+
3+
Rails.application.configure do
4+
# Verifies that versions and hashed value of the package contents in the project's package.json
5+
config.webpacker.check_yarn_integrity = false
6+
7+
# Settings specified here will take precedence over those in config/application.rb.
8+
9+
# Code is not reloaded between requests.
10+
config.cache_classes = true
11+
12+
# Eager load code on boot. This eager loads most of Rails and
13+
# your application in memory, allowing both threaded web servers
14+
# and those relying on copy on write to perform better.
15+
# Rake tasks automatically ignore this option for performance.
16+
config.eager_load = true
17+
18+
# Full error reports are disabled and caching is turned on.
19+
config.consider_all_requests_local = false
20+
config.action_controller.perform_caching = true
21+
22+
# Enable Rack::Cache to put a simple HTTP cache in front of your application
23+
# Add `rack-cache` to your Gemfile before enabling this.
24+
# For large-scale production use, consider using a caching reverse proxy like
25+
# NGINX, varnish or squid.
26+
# config.action_dispatch.rack_cache = true
27+
config.read_encrypted_secrets = true
28+
29+
# Disable serving static files from the `/public` folder by default since
30+
# Apache or NGINX already handles this.
31+
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
32+
config.public_file_server.headers = {
33+
"Cache-Control" => "public, s-maxage=2592000, max-age=86400"
34+
}
35+
36+
# Compress JavaScripts and CSS.
37+
config.assets.js_compressor = Uglifier.new(harmony: true)
38+
# config.assets.css_compressor = :sass
39+
40+
# Do not fallback to assets pipeline if a precompiled asset is missed.
41+
config.assets.compile = true
42+
43+
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
44+
# yet still be able to expire them through the digest params.
45+
config.assets.digest = true
46+
47+
# `config.assets.precompile` and `config.assets.version`
48+
# have moved to config/initializers/assets.rb
49+
50+
# Specifies the header that your server uses for sending files.
51+
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
52+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
53+
54+
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
55+
# config.force_ssl = true
56+
57+
# Use the lowest log level to ensure availability of diagnostic information
58+
# when problems arise.
59+
config.log_level = :debug
60+
61+
# Prepend all log lines with the following tags.
62+
config.log_tags = [:request_id]
63+
64+
# Use a different logger for distributed setups.
65+
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
66+
67+
# Use a different cache store in production.
68+
# config.cache_store = :mem_cache_store
69+
70+
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
71+
config.action_controller.asset_host = ENV["FASTLY_CDN_URL"]
72+
config.action_mailer.perform_caching = false
73+
74+
# Ignore bad email addresses and do not raise email delivery errors.
75+
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
76+
# config.action_mailer.raise_delivery_errors = false
77+
78+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
79+
# the I18n.default_locale when a translation cannot be found).
80+
config.i18n.fallbacks = [I18n.default_locale]
81+
82+
# Send deprecation notices to registered listeners.
83+
config.active_support.deprecation = :notify
84+
85+
# Use default logging formatter so that PID and timestamp are not suppressed.
86+
# config.log_formatter = ::Logger::Formatter.new
87+
config.log_formatter = ::Logger::Formatter.new
88+
if ENV["RAILS_LOG_TO_STDOUT"].present?
89+
logger = ActiveSupport::Logger.new(STDOUT)
90+
logger.formatter = config.log_formatter
91+
config.logger = ActiveSupport::TaggedLogging.new(logger)
92+
end
93+
94+
# Install the Timber.io logger
95+
send_logs_to_timber = ENV["SEND_LOGS_TO_TIMBER"] || "false" # <---- set to false to stop sending dev logs to Timber.io
96+
log_device = send_logs_to_timber == "true" ? Timber::LogDevices::HTTP.new(ENV["TIMBER"]) : STDOUT
97+
logger = Timber::Logger.new(log_device)
98+
logger.level = config.log_level
99+
config.logger = ActiveSupport::TaggedLogging.new(logger)
100+
101+
# Do not dump schema after migrations.
102+
config.active_record.dump_schema_after_migration = false
103+
104+
config.cache_store = :dalli_store,
105+
(ENV["MEMCACHIER_SERVERS"] || "").split(","),
106+
{ username: ENV["MEMCACHIER_USERNAME"],
107+
password: ENV["MEMCACHIER_PASSWORD"],
108+
failover: true,
109+
socket_timeout: 1.5,
110+
socket_failure_delay: 0.2 }
111+
112+
config.app_domain = "dev.to"
113+
114+
config.action_mailer.delivery_method = :smtp
115+
config.action_mailer.perform_deliveries = true
116+
config.action_mailer.default_url_options = { host: config.app_domain }
117+
ActionMailer::Base.smtp_settings = {
118+
address: "smtp.sendgrid.net",
119+
port: "587",
120+
authentication: :plain,
121+
user_name: ENV["SENDGRID_USERNAME_ACCEL"],
122+
password: ENV["SENDGRID_PASSWORD_ACCEL"],
123+
domain: "dev.to",
124+
enable_starttls_auto: true
125+
}
126+
127+
config.middleware.use Rack::HostRedirect,
128+
"practicaldev.herokuapp.com" => "dev.to"
129+
end
130+
131+
# rubocop:enable Metrics/BlockLength

config/initializers/airbrake.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
# environments.
4242
# NOTE: This option *does not* work if you don't set the 'environment' option.
4343
# https://github.com/airbrake/airbrake-ruby#ignore_environments
44-
c.ignore_environments = %w[test development]
44+
c.ignore_environments = %w[test development local_production]
4545

4646
# A list of parameters that should be filtered out of what is sent to
4747
# Airbrake. By default, all "password" attributes will have their contents

config/secrets.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
development:
1414
secret_key_base: a60edc976c913b19fd9fc8118936fbe1df2b07f4eecc5ad32f975e33cd4ea36b150c1ce933b681b90874a46568041629003dcbfc07238f7dca91741bcd1ec870
1515

16+
local_production:
17+
secret_key_base: a60edc976c913b19fd9fc8118936fbe1df2b07f4eecc5ad32f975e33cd4ea36b150c1ce933b681b90874a46568041629003dcbfc07238f7dca91741bcd1ec870
18+
1619
test:
1720
secret_key_base: 42dd7834039ebbea271af22635a6782ee15e519b14629c5276bfcdd4cff841e9926994784bb43a335a8f8c9739bb254ea3afe831839d4dc65654ec7516ec25f0
1821

config/webpacker.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,12 @@ production:
5454

5555
# Cache manifest.json for performance
5656
cache_manifest: true
57+
58+
local_production:
59+
<<: *default
60+
61+
# Production depends on precompilation of packs prior to booting for performance.
62+
compile: false
63+
64+
# Cache manifest.json for performance
65+
cache_manifest: true

0 commit comments

Comments
 (0)