From 2cf15b549e985f090648b449ca660b6d6e264f50 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 22 Jun 2008 12:10:32 +0000 Subject: [PATCH] Add support for putting the site in an offline mode where it operates without a database but with most features disabled. --- app/controllers/application.rb | 14 ++++++++++++-- app/controllers/diary_entry_controller.rb | 1 + app/controllers/trace_controller.rb | 5 ++++- app/controllers/user_controller.rb | 2 ++ app/views/layouts/site.rhtml | 4 ++-- app/views/site/edit.rhtml | 4 ++-- app/views/site/offline.rhtml | 3 +++ config/environment.rb | 15 +++++++++++---- config/routes.rb | 1 + 9 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 app/views/site/offline.rhtml diff --git a/app/controllers/application.rb b/app/controllers/application.rb index acb2f9162..7570fd82a 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -2,6 +2,10 @@ # Likewise, all the methods added will be available for all controllers. class ApplicationController < ActionController::Base + if OSM_STATUS == :database_offline + session :off + end + def authorize_web if session[:user] @user = User.find(session[:user]) @@ -39,8 +43,14 @@ class ApplicationController < ActionController::Base end end + def check_database_availability + if OSM_STATUS == :database_offline + redirect_to :controller => 'site', :action => 'offline' + end + end + def check_read_availability - if API_STATUS == :offline + if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline response.headers['Error'] = "Database offline for maintenance" render :nothing => true, :status => :service_unavailable return false @@ -48,7 +58,7 @@ class ApplicationController < ActionController::Base end def check_write_availability - if API_STATUS == :offline or API_STATUS == :readonly + if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly response.headers['Error'] = "Database offline for maintenance" render :nothing => true, :status => :service_unavailable return false diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index a3b37b931..5159f7362 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -3,6 +3,7 @@ class DiaryEntryController < ApplicationController before_filter :authorize_web before_filter :require_user, :only => [:new] + before_filter :check_database_availability def new @title = 'new diary entry' diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index cf35d0046..0467e66ba 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -1,7 +1,10 @@ class TraceController < ApplicationController + layout 'site' + before_filter :authorize_web before_filter :authorize, :only => [:api_details, :api_data, :api_create] - layout 'site' + before_filter :check_database_availability, :except => [:api_details, :api_data, :api_create] + before_filter :check_read_availability, :only => [:api_details, :api_data, :api_create] # Counts and selects pages of GPX traces for various criteria (by user, tags, public etc.). # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 31955d019..5d26708fe 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -4,6 +4,8 @@ class UserController < ApplicationController before_filter :authorize, :only => [:api_details, :api_gpx_files] before_filter :authorize_web, :only => [:account, :go_public, :view, :diary, :make_friend, :remove_friend, :upload_image] before_filter :require_user, :only => [:set_home, :account, :go_public, :make_friend, :remove_friend, :upload_image] + before_filter :check_database_availability, :except => [:api_details, :api_gpx_files] + before_filter :check_read_availability, :only => [:api_details, :api_gpx_files] filter_parameter_logging :password, :pass_crypt, :pass_crypt_confirmation diff --git a/app/views/layouts/site.rhtml b/app/views/layouts/site.rhtml index 7283ebb32..52e7b6b24 100644 --- a/app/views/layouts/site.rhtml +++ b/app/views/layouts/site.rhtml @@ -76,12 +76,12 @@ <% end %> - <% if API_STATUS == :offline %> + <% if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline %>
The OpenStreetMap database is currently offline while essential database maintenance work is carried out.
- <% elsif API_STATUS == :readonly %> + <% elsif OSM_STATUS == :api_readonly %>
The OpenStreetMap database is currently in read-only mode while essential database maintenance work is carried out. diff --git a/app/views/site/edit.rhtml b/app/views/site/edit.rhtml index afd33e4ae..a3896bc48 100644 --- a/app/views/site/edit.rhtml +++ b/app/views/site/edit.rhtml @@ -1,8 +1,8 @@ -<% if API_STATUS == :offline %> +<% if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline %>

The OpenStreetMap database is currently offline while essential database maintenance work is carried out.

-<% elsif API_STATUS == :readonly %> +<% elsif OSM_STATUS == :api_readonly %>

The OpenStreetMap database is currently in read-only mode while essential database maintenance work is carried out.

diff --git a/app/views/site/offline.rhtml b/app/views/site/offline.rhtml new file mode 100644 index 000000000..d97a6ca90 --- /dev/null +++ b/app/views/site/offline.rhtml @@ -0,0 +1,3 @@ +

The OpenStreetMap database is currently offline while + essential database maintenance work is carried out. +

diff --git a/config/environment.rb b/config/environment.rb index cfabe365f..495f94d80 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -13,9 +13,14 @@ SERVER_URL = ENV['OSM_SERVER_URL'] || 'www.openstreetmap.org' # Application constants needed for routes.rb - must go before Initializer call API_VERSION = ENV['OSM_API_VERSION'] || '0.5' -# Set to :readonly to put the API in read-only mode or :offline to -# take it completely offline -API_STATUS = :online +# Set application status - possible settings are: +# +# :online - online and operating normally +# :api_readonly - site online but API in read-only mode +# :api_offline - site online but API offline +# :database_offline - database offline with site in emergency mode +# +OSM_STATUS = :online # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') @@ -28,7 +33,9 @@ Rails::Initializer.run do |config| # Skip frameworks you're not going to use (only works if using vendor/rails). # To use Rails without a database, you must remove the Active Record framework - # config.frameworks -= [ :active_record, :active_resource, :action_mailer ] + if OSM_STATUS == :database_offline + config.frameworks -= [ :active_record ] + end # Only load the plugins named here, in the order given. By default, all plugins # in vendor/plugins are loaded in alphabetical order. diff --git a/config/routes.rb b/config/routes.rb index dc26259fa..290ad6e08 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,6 +64,7 @@ ActionController::Routing::Routes.draw do |map| map.connect '/export', :controller => 'site', :action => 'export' map.connect '/login', :controller => 'user', :action => 'login' map.connect '/logout', :controller => 'user', :action => 'logout' + map.connect '/offline', :controller => 'site', :action => 'offline' map.connect '/user/new', :controller => 'user', :action => 'new' map.connect '/user/save', :controller => 'user', :action => 'save' map.connect '/user/confirm', :controller => 'user', :action => 'confirm' -- 2.43.2