Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add config.api_only, set it to false in case you want to use Rails de…

…fault middleware stack

Closes #1
  • Loading branch information...
commit 46a5358a01b24333ddd59d7fffdca0ea24470dbb 1 parent 6e52c2a
Santiago Pastorino spastorino authored
1  CHANGELOG.md
View
@@ -1,5 +1,6 @@
## master
+* Add config.api_only, set it to false in case you want to use Rails default middleware stack. (@spastorino)
* Return proper format on exceptions. (@spastorino)
* rails-api needs Rails >= 3.2.2. (@scottnichol)
2  README.md
View
@@ -107,6 +107,8 @@ end
And comment out the `protect_from_forgery` call if you are using it.
+If you want to use the Rails default middleware stack (avoid the reduction that rails-api does), you can just add config.api_only = false to config/application.rb file.
+
### Choosing Middlewares
An API application comes with the following middlewares by default.
15 lib/rails-api/application.rb
View
@@ -25,6 +25,7 @@ def default_middleware_stack
middleware.use ::Rack::Lock unless config.allow_concurrency
middleware.use ::Rack::Runtime
+ middleware.use ::Rack::MethodOverride unless config.api_only
middleware.use ::ActionDispatch::RequestId
middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || Rails::API::PublicExceptions.new(Rails.public_path)
@@ -37,11 +38,24 @@ def default_middleware_stack
end
middleware.use ::ActionDispatch::Callbacks
+ middleware.use ::ActionDispatch::Cookies unless config.api_only
+
+ if !config.api_only && config.session_store
+ if config.force_ssl && !config.session_options.key?(:secure)
+ config.session_options[:secure] = true
+ end
+ middleware.use config.session_store, config.session_options
+ middleware.use ::ActionDispatch::Flash
+ end
middleware.use ::ActionDispatch::ParamsParser
middleware.use ::ActionDispatch::Head
middleware.use ::Rack::ConditionalGet
middleware.use ::Rack::ETag, "no-cache"
+
+ if !config.api_only && config.action_dispatch.best_standards_support
+ middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
+ end
end
end
@@ -98,6 +112,7 @@ def setup_generators!
end
ActiveSupport.on_load(:before_configuration) do
+ config.api_only = true
setup_generators!
end
end
80 lib/rails-api/templates/rails/app/config/application.rb
View
@@ -0,0 +1,80 @@
+require File.expand_path('../boot', __FILE__)
+
+<% if include_all_railties? -%>
+require 'rails/all'
+<% else -%>
+# Pick the frameworks you want:
+<%= comment_if :skip_active_record %>require "active_record/railtie"
+require "action_controller/railtie"
+require "action_mailer/railtie"
+<% if Rails::VERSION::STRING < "4.0.0" %>require "active_resource/railtie"<% end %>
+<%= comment_if :skip_sprockets %>require "sprockets/rails/railtie"
+<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie"
+<% end -%>
+
+if defined?(Bundler)
+ # If you precompile assets before deploying to production, use this line.
+ Bundler.require(*Rails.groups(:assets => %w(development test)))
+ # If you want your assets lazily compiled in production, use this line.
+ # Bundler.require(:default, :assets, Rails.env)
+end
+
+module <%= app_const_base %>
+ class Application < Rails::Application
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+
+ # Custom directories with classes and modules you want to be autoloadable.
+ # config.autoload_paths += %W(#{config.root}/extras)
+
+ # Activate observers that should always be running.
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
+
+ # Configure the default encoding used in templates for Ruby 1.9.
+ config.encoding = "utf-8"
+
+ # Configure sensitive parameters which will be filtered from the log file.
+ config.filter_parameters += [:password]
+
+ # Enable escaping HTML in JSON.
+ config.active_support.escape_html_entities_in_json = true
+
+ # 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
+
+ # Enforce whitelist mode for mass assignment.
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
+ # parameters by using an attr_accessible or attr_protected declaration.
+ <%= comment_if :skip_active_record %>config.active_record.whitelist_attributes = true
+
+<% if Rails::VERSION::STRING >= "4.0.0" -%>
+ # Specifies whether or not has_many or has_one association option :dependent => :restrict raises
+ # an exception. If set to true, then an ActiveRecord::DeleteRestrictionError exception would be
+ # raised. If set to false, then an error will be added on the model instead.
+ <%= comment_if :skip_active_record %>config.active_record.dependent_restrict_raises = false
+<% end -%>
+<% unless options.skip_sprockets? -%>
+
+ # Enable the asset pipeline.
+ config.assets.enabled = true
+
+ # Version of your assets, change this if you want to expire all your assets.
+ config.assets.version = '1.0'
+<% end -%>
+
+ # Turn off the middleware reduction and use Rails default middleware stack
+ # config.api_only = false
+ end
+end
24 test/api_application/api_application_test.rb
View
@@ -2,6 +2,8 @@
require 'action_controller/railtie'
require 'rack/test'
+app.initialize!
+
class OmgController < ActionController::API
def index
render :text => "OMG"
@@ -12,10 +14,28 @@ class ApiApplicationTest < ActiveSupport::TestCase
include ::Rack::Test::Methods
def test_boot_api_app
- app.initialize!
-
get "/omg"
assert_equal 200, last_response.status
assert_equal "OMG", last_response.body
end
+
+ def test_api_middleware_stack
+ assert_equal [
+ "ActionDispatch::Static",
+ "Rack::Lock",
+ "ActiveSupport::Cache::Strategy::LocalCache",
+ "Rack::Runtime",
+ "ActionDispatch::RequestId",
+ "Rails::Rack::Logger",
+ "ActionDispatch::ShowExceptions",
+ "ActionDispatch::DebugExceptions",
+ "ActionDispatch::RemoteIp",
+ "ActionDispatch::Reloader",
+ "ActionDispatch::Callbacks",
+ "ActionDispatch::ParamsParser",
+ "ActionDispatch::Head",
+ "Rack::ConditionalGet",
+ "Rack::ETag"
+ ], app.middleware.map(&:klass).map(&:name)
+ end
end
1  test/generators/app_generator_test.rb
View
@@ -23,6 +23,7 @@ def test_api_modified_files
assert_no_match(/gem 'sass-rails'/, content)
end
assert_file "app/controllers/application_controller.rb", /ActionController::API/
+ assert_file "config/application.rb", /# config.api_only = false/
end
private

4 comments on commit 46a5358

Carlos Antonio da Silva

Great bro! :heart:

I wonder if we really need to have our own config/application.rb, or just the readme entry would do for this info.
Thanks!

Santiago Pastorino
Owner

I agree I think we shouldn't just for that :)

Santiago Pastorino
Owner

done 5bbf22e

Please sign in to comment.
Something went wrong with that request. Please try again.