github.com/prutya/cors

Crystal CORS handler (middleware)

4 stars
1 dependent
License: MIT

Nothing has been indexed for 0.2.3 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  cors:
    github: prutya/cors
    version: ~> 0.2.3

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.2.3. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

Crystal CORS handler

CORS is a HTTP::Handler handler (middleware) implementing Cross Origin Resource Sharing W3 specification in Crystal.

Installation

In shards.yml:

dependencies:
  cors:
    github: prutya/cors
    branch: master # OR: version: x.x.x (Check the releases tab)

Then:

require "cors"

Getting Started

Add this handler to your HTTP::Server initializer call

server = HTTP::Server.new([
  SomeHandler.new,
  Cors::Handler.new(
    respond_ok: ->(ctx : HTTP::Server::Context) {
      # Whatever you do when you want to respond with 200 OK
      #
      # e.g.
      ctx.response.status = HTTP::Status::OK
      ctx.response.content_type = "application/json"
      ctx.response.print("{}")
    },
    allowed_origins: ["https://example.com"]
  ),
  SomeOtherHandler.new,
])

Configuration

respond_ok (required) Proc with HTTP::Server::Context argument. Called when the preflight processing is finished.

max_age (optional) Int32, defaults to 0. If greater than 0, goes to Access-Control-Max-Age header in the preflight response, otherwise does nothing.

allow_credentials (optional) Bool, defaults to false If set to true, adds the Access-Control-Allow-Credentials header to the response, otherwise does nothing.

allowed_origins (optional) Array(String). Defaults to an empty array. The list of allowed origins. If the array contains "*", allows all origins. Access-Control-Allow-Origin.

allowed_methods (optional) Array(String). Defaults to ["GET", "POST", "PUT"]. The list of allowed HTTP methods. If empty, results in all requests being disallowed. Access-Control-Allow-Methods.

allowed_headers (optional) Array(String). Defaults to ["Origin", "Accept", "Content-Type", "X-Requested-With"]. If empty, results in only Origin header being allowed, otherwise results in Origin header and headers provided being allowed. Access-Control-Allow-Headers.

exposed_headers (optional) Array(String). Defaults to and empty array. Access-Control-Expose-Headers.

Benchmarks (TBD)