Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
- Change to use Markdown
- Updated use call to insert_before for Rails configuration

Fixes #32
  • Loading branch information
cyu committed Sep 7, 2014
1 parent 2251fe2 commit c27465a
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Rack CORS Middleware

`Rack::Cors` provides support for Cross-Origin Resource Sharing (CORS) for Rack compatible web applications. The CORS spec allows web applications to make cross domain AJAX calls without
using workarounds such as JSONP. For a thorough write up on CORS, see this blog post:

http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

Or for all the gory details, you can read the spec here:

http://www.w3.org/TR/cors/

## Installation

Install the gem:

`gem install rack-cors`

Or in your Gemfile:

```ruby
gem 'rack-cors', :require => 'rack/cors'
```


## Configuration

### Rack

You configure `Rack::Cors` by passing a block to the `use` command:

```ruby
use Rack::Cors do
allow do
origins 'localhost:3000', '127.0.0.1:3000',
/http:\/\/192\.168\.0\.\d{1,3}(:\d+)?/
# regular expressions can be used here

resource '/file/list_all/', :headers => 'x-domain-token'
resource '/file/at/*',
:methods => [:get, :post, :put, :delete, :options],
:headers => 'x-domain-token',
:expose => ['Some-Custom-Response-Header'],
:max_age => 600
# headers to expose
end

allow do
origins '*'
resource '/public/*', :headers => :any, :methods => :get
end
end
```

### Rails
Put something like the code below in `config/application.rb` of your Rails application. For example, this will allow GET, POST or OPTIONS requests from any origin on any resource.

```ruby
module YourApp
class Application < Rails::Application

# ...

config.middleware.insert_before "ActionDispatch::Static", "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end

end
end
```

Refer to `examples/rails3` and `examples/rails4` for more details.

See http://guides.rubyonrails.org/rails_on_rack.html for more details on rack middlewares or
http://railscasts.com/episodes/151-rack-middleware.

4 comments on commit c27465a

@espen
Copy link

@espen espen commented on c27465a Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this Markdown or Standard Markdown?

@cyu
Copy link
Owner Author

@cyu cyu commented on c27465a Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - educate me on the difference?

@espen
Copy link

@espen espen commented on c27465a Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyu
Copy link
Owner Author

@cyu cyu commented on c27465a Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jesus - good timing on my part. Thanks for the link.

Please sign in to comment.