diff --git a/Gemfile b/Gemfile index f44f4536..f2aae243 100644 --- a/Gemfile +++ b/Gemfile @@ -1,34 +1,38 @@ source "https://rubygems.org" -gem "json" gem "rails", "~> 5.1" -gem "unicorn" - gem "mysql2", "~> 0.3.20" # uncomment to use PostgreSQL # gem "pg" +# rails gem 'scenic' gem 'scenic-mysql' +gem "activerecord-typedstore" -gem "uglifier", ">= 1.3.0" -gem "jquery-rails", "~> 4.3" +# js gem "dynamic_form" +gem "jquery-rails", "~> 4.3" +gem "json" +gem "uglifier", ">= 1.3.0" +# deployment +gem "actionpack-page_caching" gem "exception_notification" +gem "unicorn" +# security gem "bcrypt", "~> 3.1.2" gem "rotp" gem "rqrcode" +# parsing gem "nokogiri", ">= 1.7.2" gem "htmlentities" gem "commonmarker", "~> 0.14" -gem "activerecord-typedstore" - # for twitter-posting bot gem "oauth" diff --git a/Gemfile.lock b/Gemfile.lock index 2afcb67e..17d7f3e6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,6 +18,8 @@ GEM rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionpack-page_caching (1.1.0) + actionpack (>= 4.0.0, < 6) actionview (5.1.5) activesupport (= 5.1.5) builder (~> 3.1) @@ -194,6 +196,7 @@ PLATFORMS ruby DEPENDENCIES + actionpack-page_caching activerecord-typedstore bcrypt (~> 3.1.2) bullet diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d46c03f8..cc0fc5b7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,8 +5,11 @@ class ApplicationController < ActionController::Base TRAFFIC_DECREMENTER = 0.50 + # match this in your nginx config for bypassing the file cache TAG_FILTER_COOKIE = :tag_filters + CACHE_PAGE = proc { @user.blank? && cookies[TAG_FILTER_COOKIE].blank? } + def authenticate_user # eagerly evaluate, in case this triggers an IpSpoofAttackError request.remote_ip diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 75a2a486..cd134389 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,6 +1,8 @@ class CommentsController < ApplicationController COMMENTS_PER_PAGE = 20 + caches_page :index, :threads, if: CACHE_PAGE + # for rss feeds, load the user's tag filters if a token is passed before_action :find_user_from_rss_token, :only => [:index] before_action :require_logged_in_user_or_400, diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index dffec183..74d4c913 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,4 +1,6 @@ class HomeController < ApplicationController + caches_page :about, :chat, :index, :newest, :newest_by_user, :recent, :top, if: CACHE_PAGE + # for rss feeds, load the user's tag filters if a token is passed before_action :find_user_from_rss_token, :only => [:index, :newest, :saved] before_action { @page = page } diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 43dfa19d..ce9a603a 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -1,4 +1,6 @@ class StoriesController < ApplicationController + caches_page :show, if: CACHE_PAGE + before_action :require_logged_in_user_or_400, :only => [:upvote, :downvote, :unvote, :hide, :unhide, :preview, :save, :unsave] before_action :require_logged_in_user, diff --git a/config/environments/production.rb b/config/environments/production.rb index 2179e9c0..3ac017df 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -75,6 +75,8 @@ Lobsters::Application.configure do # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + config.action_controller.page_cache_directory = "#{Rails.root}/public/cache" end %w{render_template render_partial render_collection}.each do |event| diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb index 95ea4e96..a34e67ec 100644 --- a/config/initializers/session_store.rb +++ b/config/initializers/session_store.rb @@ -1,5 +1,6 @@ # Be sure to restart your server when you modify this file. +# match this in your nginx config for bypassing the file cache Lobsters::Application.config.session_store :cookie_store, key: 'lobster_trap', expire_after: 1.month diff --git a/public/cache/.gitkeep b/public/cache/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/script/expire_page_cache b/script/expire_page_cache new file mode 100755 index 00000000..ce14fccd --- /dev/null +++ b/script/expire_page_cache @@ -0,0 +1,4 @@ +#!/bin/sh + +find /srv/lobste.rs/http/public/cache/ -type f -not -mmin 2 -not -name ".gitkeep" -delete +find /srv/lobste.rs/http/public/cache/ -type d -empty -delete