Move status into the settings object

Only the very early boot code needs to look at the value
from the environment directly.
This commit is contained in:
Tom Hughes 2019-03-16 16:29:12 +00:00
parent 6600221fe3
commit 141df02e67
14 changed files with 49 additions and 55 deletions

View file

@ -10,7 +10,7 @@ OSM = {
SERVER_PROTOCOL: <%= Settings.server_protocol.to_json %>,
SERVER_URL: <%= Settings.server_url.to_json %>,
API_VERSION: <%= Settings.api_version.to_json %>,
STATUS: <%= STATUS.to_json %>,
STATUS: <%= Settings.status.to_json %>,
MAX_NOTE_REQUEST_AREA: <%= Settings.max_note_request_area.to_json %>,
OVERPASS_URL: <%= Settings.overpass_url.to_json %>,
NOMINATIM_URL: <%= Settings.nominatim_url.to_json %>,

View file

@ -163,7 +163,7 @@ module Api
end
def offline_redirect
redirect_to :action => :offline if STATUS == :gpx_offline
redirect_to :action => :offline if Settings.status == "gpx_offline"
end
end
end

View file

@ -126,7 +126,7 @@ class ApplicationController < ActionController::Base
end
def check_database_readable(need_api = false)
if STATUS == :database_offline || (need_api && STATUS == :api_offline)
if Settings.status == "database_offline" || (need_api && Settings.status == "api_offline")
if request.xhr?
report_error "Database offline for maintenance", :service_unavailable
else
@ -136,8 +136,8 @@ class ApplicationController < ActionController::Base
end
def check_database_writable(need_api = false)
if STATUS == :database_offline || STATUS == :database_readonly ||
(need_api && (STATUS == :api_offline || STATUS == :api_readonly))
if Settings.status == "database_offline" || Settings.status == "database_readonly" ||
(need_api && (Settings.status == "api_offline" || Settings.status == "api_readonly"))
if request.xhr?
report_error "Database offline for maintenance", :service_unavailable
else
@ -161,9 +161,9 @@ class ApplicationController < ActionController::Base
end
def database_status
if STATUS == :database_offline
if Settings.status == "database_offline"
:offline
elsif STATUS == :database_readonly
elsif Settings.status == "database_readonly"
:readonly
else
:online
@ -173,9 +173,9 @@ class ApplicationController < ActionController::Base
def api_status
status = database_status
if status == :online
if STATUS == :api_offline
if Settings.status == "api_offline"
status = :offline
elsif STATUS == :api_readonly
elsif Settings.status == "api_readonly"
status = :readonly
end
end
@ -184,7 +184,7 @@ class ApplicationController < ActionController::Base
def gpx_status
status = database_status
status = :offline if status == :online && STATUS == :gpx_offline
status = :offline if status == :online && Settings.status == "gpx_offline"
status
end
@ -338,9 +338,9 @@ class ApplicationController < ActionController::Base
:style_src => %w['unsafe-inline']
)
if STATUS == :database_offline || STATUS == :api_offline
if Settings.status == "database_offline" || Settings.status == "api_offline"
flash.now[:warning] = t("layouts.osm_offline")
elsif STATUS == :database_readonly || STATUS == :api_readonly
elsif Settings.status == "database_readonly" || Settings.status == "api_readonly"
flash.now[:warning] = t("layouts.osm_read_only")
end

View file

@ -12,7 +12,7 @@ class SiteController < ApplicationController
authorize_resource :class => false
def index
session[:location] ||= OSM.ip_location(request.env["REMOTE_ADDR"]) unless STATUS == :database_readonly || STATUS == :database_offline
session[:location] ||= OSM.ip_location(request.env["REMOTE_ADDR"]) unless Settings.status == "database_readonly" || Settings.status == "database_offline"
end
def permalink

View file

@ -323,11 +323,11 @@ class TracesController < ApplicationController
end
def offline_warning
flash.now[:warning] = t "traces.offline_warning.message" if STATUS == :gpx_offline
flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
end
def offline_redirect
redirect_to :action => :offline if STATUS == :gpx_offline
redirect_to :action => :offline if Settings.status == "gpx_offline"
end
def default_visibility

View file

@ -1,7 +1,7 @@
<% content_for :content do %>
<% if STATUS == :database_offline or STATUS == :api_offline %>
<% if Settings.status == "database_offline" or Settings.status == "api_offline" %>
<p><%= t 'layouts.osm_offline' %></p>
<% elsif STATUS == :database_readonly or STATUS == :api_readonly %>
<% elsif Settings.status == "database_readonly" or Settings.status == "api_readonly" %>
<p><%= t 'layouts.osm_read_only' %></p>
<% elsif !current_user.data_public? %>
<p><%= t '.not_public' %></p>

View file

@ -1,4 +1,4 @@
<% if STATUS == :database_offline %>
<% if Settings.status == "database_offline" %>
<p><%= t 'layouts.osm_offline' %>
</p>
<% else %>

View file

@ -1,7 +1,7 @@
<tr>
<% cl = cycle('table0', 'table1') %>
<td class="<%= cl %>">
<% if STATUS != :gpx_offline %>
<% if Settings.status != "gpx_offline" %>
<% if trace.inserted %>
<a href="<%= url_for :controller => 'traces', :action => 'show', :id => trace.id, :display_name => trace.user.display_name %>"><img src="<%= url_for :controller => 'traces', :action => 'icon', :id => trace.id, :display_name => trace.user.display_name %>" border="0" alt="" /></a>
<% else %>

View file

@ -2,7 +2,7 @@
<h2><%= t '.heading', :name => h(@trace.name) %></h2>
<% end %>
<% if STATUS != :gpx_offline %>
<% if Settings.status != "gpx_offline" %>
<% if @trace.inserted %>
<img src="<%= url_for :controller => 'traces', :action => 'picture', :id => @trace.id, :display_name => @trace.user.display_name %>">
<% else %>

View file

@ -13,24 +13,7 @@ if File.exist?(File.expand_path("application.yml", __dir__))
end
# rubocop:enable Rails/Output, Rails/Exit
# Set the STATUS constant from the environment, if it matches a recognized value
ALLOWED_STATUS = [
:online, # online and operating normally
:api_readonly, # site online but API in read-only mode
:api_offline, # site online but API offline
:database_readonly, # database and site in read-only mode
:database_offline, # database offline with site in emergency mode
:gpx_offline # gpx storage offline
].freeze
status = if ENV["STATUS"] && ALLOWED_STATUS.include?(ENV["STATUS"].to_sym)
ENV["STATUS"].to_sym
else
:online
end
Object.const_set("STATUS", status)
if STATUS == :database_offline
if ENV["OPENSTREETMAP_STATUS"] == "database_offline"
require "active_model/railtie"
require "active_job/railtie"
require "active_storage/engine"
@ -63,15 +46,15 @@ module OpenStreetMap
# This defaults to true from rails 5.0 but our code doesn't comply
# with it at all so we turn it off
config.active_record.belongs_to_required_by_default = false unless STATUS == :database_offline
config.active_record.belongs_to_required_by_default = false unless Settings.status == "database_offline"
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
config.active_record.schema_format = :sql unless STATUS == :database_offline
config.active_record.schema_format = :sql unless Settings.status == "database_offline"
# Don't eager load models when the database is offline
config.paths["app/models"].skip_eager_load! if STATUS == :database_offline
config.paths["app/models"].skip_eager_load! if Settings.status == "database_offline"
# Use memcached for caching if required
config.cache_store = :mem_cache_store, Settings.memcache_servers, { :namespace => "rails:cache" } if Settings.key?(:memcache_servers)

View file

@ -39,10 +39,10 @@ Rails.application.configure do
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load unless STATUS == :database_offline
config.active_record.migration_error = :page_load unless Settings.status == "database_offline"
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true unless STATUS == :database_offline
config.active_record.verbose_query_logs = true unless Settings.status == "database_offline"
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large

View file

@ -95,7 +95,7 @@ Rails.application.configure do
end
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false unless STATUS == :database_offline
config.active_record.dump_schema_after_migration = false unless Settings.status == "database_offline"
# Enable autoloading of dependencies.
config.enable_dependency_loading = true

View file

@ -1,3 +1,13 @@
# Allowed status values
ALLOWED_STATUS ||= [
"online", # online and operating normally
"api_readonly", # site online but API in read-only mode
"api_offline", # site online but API offline
"database_readonly", # database and site in read-only mode
"database_offline", # database offline with site in emergency mode
"gpx_offline" # gpx storage offline
].freeze
Config.setup do |config|
# Name of the constant exposing loaded settings
config.const_name = "Settings"
@ -17,23 +27,23 @@ Config.setup do |config|
# Load environment variables from the `ENV` object and override any settings defined in files.
#
# config.use_env = false
config.use_env = true
# Define ENV variable prefix deciding which variables to load into config.
#
# config.env_prefix = 'Settings'
config.env_prefix = "OPENSTREETMAP"
# What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well
# with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where
# using dots in variable names might not be allowed (eg. Bash).
#
# config.env_separator = '.'
config.env_separator = "_"
# Ability to process variables names:
# * nil - no change
# * :downcase - convert to lower case
#
# config.env_converter = :downcase
config.env_converter = :downcase
# Parse numeric values as integers instead of strings.
#
@ -49,5 +59,6 @@ Config.setup do |config|
required(:max_number_of_way_nodes).filled(:int?)
required(:api_timeout).filled(:int?)
required(:imagery_blacklist).maybe(:array?)
required(:status).filled(:str?, :included_in? => ALLOWED_STATUS)
end
end

View file

@ -16,13 +16,13 @@ email_return_path: "openstreetmap@example.com"
# API version
api_version: "0.6"
# Application status - possible values are:
# :online - online and operating normally
# :api_readonly - site online but API in read-only mode
# :api_offline - site online but API offline
# :database_readonly - database and site in read-only mode
# :database_offline - database offline with site in emergency mode
# :gpx_offline - gpx storage offline
status: :online
# online - online and operating normally
# api_readonly - site online but API in read-only mode
# api_offline - site online but API offline
# database_readonly - database and site in read-only mode
# database_offline - database offline with site in emergency mode
# gpx_offline - gpx storage offline
status: "online"
# The maximum area you're allowed to request, in square degrees
max_request_area: 0.25
# Number of GPS trace/trackpoints returned per-page