module Tennpipes::ApplicationSetup::ClassMethods
Public Instance Methods
Defines default settings for Tennpipes
application.
# File lib/tennpipes-base/application/application_setup.rb, line 14 def default_configuration! set :app_file, File.expand_path(caller_files.first || $0) set :app_name, settings.to_s.underscore.to_sym set :environment, Tennpipes.env set :reload, proc { development? } set :logging, proc { development? } set :method_override, true set :default_builder, 'StandardFormBuilder' default_paths default_security global_configuration setup_prerequisites end
# File lib/tennpipes-base/application/application_setup.rb, line 51 def precompile_routes! compiled_router.prepare! compiled_router.engine.compile! end
# File lib/tennpipes-base/application/application_setup.rb, line 47 def precompile_routes? settings.respond_to?(:precompile_routes) && settings.precompile_routes? end
Setup the application by registering initializers, load paths and logger. Invoked automatically when an application is first instantiated.
@return [TrueClass]
# File lib/tennpipes-base/application/application_setup.rb, line 37 def setup_application! return if @_configured require_dependencies default_routes default_errors setup_locale precompile_routes! @_configured = true end
Private Instance Methods
warn if the protect_from_csrf is active but sessions are not
# File lib/tennpipes-base/application/application_setup.rb, line 181 def check_csrf_protection_dependency if (protect_from_csrf? && !sessions?) && !defined?(Tennpipes::IGNORE_CSRF_SETUP_WARNING) warn(<<-ERROR) `protect_from_csrf` is activated, but `sessions` seem to be off. To enable csrf protection, use: enable :sessions or deactivate protect_from_csrf: disable :protect_from_csrf If you use a different session store, ignore this warning using: # in boot.rb: Tennpipes::IGNORE_CSRF_SETUP_WARNING = true ERROR end end
This log errors for production environments.
# File lib/tennpipes-base/application/application_setup.rb, line 132 def default_errors configure :production do error ::Exception do logger.exception env['sinatra.error'] halt(500, { 'Content-Type' => 'text/html' }, ['<h1>Internal Server Error</h1>']) end unless errors.has_key?(::Exception) end end
# File lib/tennpipes-base/application/application_setup.rb, line 58 def default_paths set :locale_path, proc { Dir.glob File.join(root, 'locale/**/*.{rb,yml}') } set :views, proc { File.join(root, 'views') } set :uri_root, '/' set :public_folder, proc { Tennpipes.root('public', uri_root) } set :images_path, proc { File.join(public_folder, 'images') } end
Returns globs of default paths of application prerequisites.
# File lib/tennpipes-base/application/application_setup.rb, line 92 def default_prerequisites [ '/models.rb', '/models/**/*.rb', '/lib.rb', '/lib/**/*.rb', ].map{ |glob| File.join(settings.root, glob) } end
We need to add almost __sinatra__ images.
# File lib/tennpipes-base/application/application_setup.rb, line 120 def default_routes configure :development do get '*__sinatra__/:image.png' do content_type :png send_file(File.dirname(__FILE__) + "/../images/#{params[:image]}.png") end end end
# File lib/tennpipes-base/application/application_setup.rb, line 67 def default_security set :protection, :except => :path_traversal set :sessions, false set :protect_from_csrf, false set :report_csrf_failure, false set :allow_disabled_csrf, false end
Applies global tennpipes configuration blocks to current application.
# File lib/tennpipes-base/application/application_setup.rb, line 78 def global_configuration Tennpipes.global_configurations.each do |configuration| class_eval(&configuration) end end
returns the options used in the builder for csrf protection setup
# File lib/tennpipes-base/application/application_setup.rb, line 169 def options_for_csrf_protection_setup options = { :logger => logger } if report_csrf_failure? || allow_disabled_csrf? options.merge!( :reaction => :report, :report_key => 'protection.csrf.failed' ) end options end
sets up csrf protection for the app
# File lib/tennpipes-base/application/application_setup.rb, line 158 def setup_csrf_protection(builder) check_csrf_protection_dependency if protect_from_csrf? options = options_for_csrf_protection_setup options.merge!(protect_from_csrf) if protect_from_csrf.kind_of?(Hash) builder.use(options[:except] ? Tennpipes::AuthenticityToken : Rack::Protection::AuthenticityToken, options) end end
Overrides the default middleware for Sinatra
based on Tennpipes
conventions. Also initializes the application after setting up the middleware.
# File lib/tennpipes-base/application/application_setup.rb, line 103 def setup_default_middleware(builder) setup_sessions builder builder.use Sinatra::ExtendedRack if defined?(EventMachine) builder.use Tennpipes::ShowExceptions if show_exceptions? builder.use Tennpipes::Logger::Rack, uri_root if Tennpipes.logger && logging? builder.use Tennpipes::Reloader::Rack if reload? builder.use Rack::MethodOverride if method_override? builder.use Rack::Head register Tennpipes::Flash setup_protection builder setup_csrf_protection builder setup_application! end
# File lib/tennpipes-base/application/application_setup.rb, line 141 def setup_locale return unless defined? I18n Reloader.special_files += locale_path I18n.load_path << locale_path I18n.reload! end
# File lib/tennpipes-base/application/application_setup.rb, line 84 def setup_prerequisites prerequisites.concat(default_prerequisites).uniq! Tennpipes.require_dependencies(prerequisites) end
allow custome session management
# File lib/tennpipes-base/application/application_setup.rb, line 149 def setup_sessions(builder) if sessions.kind_of?(Hash) && sessions[:use] builder.use sessions[:use], sessions[:config] || {} else super end end