FIX: add ReadOnly hear to /srv/status

This commit is contained in:
Régis Hanol 2019-05-03 12:21:07 +02:00
parent 16ef060a76
commit 0d41d58d27
4 changed files with 44 additions and 8 deletions

View File

@ -14,6 +14,7 @@ require_dependency 'global_path'
require_dependency 'secure_session' require_dependency 'secure_session'
require_dependency 'topic_query' require_dependency 'topic_query'
require_dependency 'hijack' require_dependency 'hijack'
require_dependency 'read_only'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
include CurrentUser include CurrentUser
@ -21,6 +22,7 @@ class ApplicationController < ActionController::Base
include JsonError include JsonError
include GlobalPath include GlobalPath
include Hijack include Hijack
include ReadOnly
attr_reader :theme_ids attr_reader :theme_ids
@ -80,10 +82,6 @@ class ApplicationController < ActionController::Base
(has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent) || params.key?("print")) (has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent) || params.key?("print"))
end end
def add_readonly_header
response.headers['Discourse-Readonly'] = 'true' if @readonly_mode
end
def perform_refresh_session def perform_refresh_session
refresh_session(current_user) unless @readonly_mode refresh_session(current_user) unless @readonly_mode
end end
@ -508,10 +506,6 @@ class ApplicationController < ActionController::Base
private private
def check_readonly_mode
@readonly_mode = Discourse.readonly_mode?
end
def locale_from_header def locale_from_header
begin begin
# Rails I18n uses underscores between the locale and the region; the request # Rails I18n uses underscores between the locale and the region; the request

View File

@ -1,6 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "read_only"
class ForumsController < ActionController::Base class ForumsController < ActionController::Base
include ReadOnly
before_action :check_readonly_mode
after_action :add_readonly_header
def status def status
if $shutdown if $shutdown
render plain: 'shutting down', status: 500 render plain: 'shutting down', status: 500

13
lib/read_only.rb Normal file
View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
module ReadOnly
def check_readonly_mode
@readonly_mode = Discourse.readonly_mode?
end
def add_readonly_header
response.headers['Discourse-Readonly'] = 'true' if @readonly_mode
end
end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ForumsController do
describe "read only header" do
it "returns no read only header by default" do
get "/srv/status"
expect(response.status).to eq(200)
expect(response.headers['Discourse-Readonly']).to eq(nil)
end
it "returns a readonly header if the site is read only" do
Discourse.received_readonly!
get "/srv/status"
expect(response.status).to eq(200)
expect(response.headers['Discourse-Readonly']).to eq('true')
end
end
end