From 141df02e671d7c559f6d813bde1d792a159f1875 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sat, 16 Mar 2019 16:29:12 +0000 Subject: [PATCH] Move status into the settings object Only the very early boot code needs to look at the value from the environment directly. --- app/assets/javascripts/osm.js.erb | 2 +- app/controllers/api/traces_controller.rb | 2 +- app/controllers/application_controller.rb | 20 +++++++++--------- app/controllers/site_controller.rb | 2 +- app/controllers/traces_controller.rb | 4 ++-- app/views/site/edit.html.erb | 4 ++-- app/views/site/offline.html.erb | 2 +- app/views/traces/_trace.html.erb | 2 +- app/views/traces/show.html.erb | 2 +- config/application.rb | 25 ++++------------------- config/environments/development.rb | 4 ++-- config/environments/production.rb | 2 +- config/initializers/config.rb | 19 +++++++++++++---- config/settings.yml | 14 ++++++------- 14 files changed, 49 insertions(+), 55 deletions(-) diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index f5d530a1b..205dae0d1 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -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 %>, diff --git a/app/controllers/api/traces_controller.rb b/app/controllers/api/traces_controller.rb index 8401e78ae..d7f2f043a 100644 --- a/app/controllers/api/traces_controller.rb +++ b/app/controllers/api/traces_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0e77a398d..8d9ee11f9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb index 5833a1e04..471e9f3c5 100644 --- a/app/controllers/site_controller.rb +++ b/app/controllers/site_controller.rb @@ -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 diff --git a/app/controllers/traces_controller.rb b/app/controllers/traces_controller.rb index 3091f4360..334aa2aa6 100644 --- a/app/controllers/traces_controller.rb +++ b/app/controllers/traces_controller.rb @@ -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 diff --git a/app/views/site/edit.html.erb b/app/views/site/edit.html.erb index e84adf2d4..d89af0596 100644 --- a/app/views/site/edit.html.erb +++ b/app/views/site/edit.html.erb @@ -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" %>

<%= t 'layouts.osm_offline' %>

- <% elsif STATUS == :database_readonly or STATUS == :api_readonly %> + <% elsif Settings.status == "database_readonly" or Settings.status == "api_readonly" %>

<%= t 'layouts.osm_read_only' %>

<% elsif !current_user.data_public? %>

<%= t '.not_public' %>

diff --git a/app/views/site/offline.html.erb b/app/views/site/offline.html.erb index 34a2eacc5..d306c2af9 100644 --- a/app/views/site/offline.html.erb +++ b/app/views/site/offline.html.erb @@ -1,4 +1,4 @@ -<% if STATUS == :database_offline %> +<% if Settings.status == "database_offline" %>

<%= t 'layouts.osm_offline' %>

<% else %> diff --git a/app/views/traces/_trace.html.erb b/app/views/traces/_trace.html.erb index b81e19025..849695e80 100644 --- a/app/views/traces/_trace.html.erb +++ b/app/views/traces/_trace.html.erb @@ -1,7 +1,7 @@ <% cl = cycle('table0', 'table1') %> - <% if STATUS != :gpx_offline %> + <% if Settings.status != "gpx_offline" %> <% if trace.inserted %> <% else %> diff --git a/app/views/traces/show.html.erb b/app/views/traces/show.html.erb index 178cfbe73..f8e8510f9 100644 --- a/app/views/traces/show.html.erb +++ b/app/views/traces/show.html.erb @@ -2,7 +2,7 @@

<%= t '.heading', :name => h(@trace.name) %>

<% end %> -<% if STATUS != :gpx_offline %> +<% if Settings.status != "gpx_offline" %> <% if @trace.inserted %> <% else %> diff --git a/config/application.rb b/config/application.rb index 096b7573a..a8a13c476 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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) diff --git a/config/environments/development.rb b/config/environments/development.rb index e850887b3..9a3976f76 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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 diff --git a/config/environments/production.rb b/config/environments/production.rb index 051c5dd44..97121d5a7 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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 diff --git a/config/initializers/config.rb b/config/initializers/config.rb index caaab0c2f..80997e0dc 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -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 diff --git a/config/settings.yml b/config/settings.yml index 4f267e5d0..5af1fb00d 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -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 -- 2.43.2