• Stars
    star
    513
  • Rank 86,178 (Top 2 %)
  • Language
    Elixir
  • License
    MIT License
  • Created over 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Elixir library for dealing with CORS requests. ๐Ÿ–

Corsica

hex.pm badge Documentation badge CI Coverage Status

Corsica is a plug and a DSL for handling CORS requests. Documentation can be found online.

Landscape image of a sunset in Corsica, with the sea in the foreground and mountains in the background

Photo by Hendrik Cornelissen on Unsplash

(I had to include a nice pic because, let's be honest, CORS requests aren't the most fun thing in the world, are they?)

Features

  • Is compliant with the W3C CORS specification
  • Provides both low-level CORS utilities as well as high-level facilities (like a built-in plug and a CORS-focused router)
  • Handles preflight requests like a breeze
  • Never sends any CORS headers if the CORS request is not valid (smaller requests, yay!)

Installation

Add the :corsica dependency to your project's mix.exs:

defp deps do
  [
    {:plug, "~> 1.0"},
    {:corsica, "~> 1.0"}
  ]
end

and then run $ mix deps.get.

Overview

You can use Corsica both as a plug as well as a router generator. To use it as a plug, just plug it into your plug pipeline:

defmodule MyApp.Endpoint do
  plug Logger
  plug Corsica, origins: "http://foo.com"
  plug MyApp.Router
end

To gain finer control over which resources are CORS-enabled and with what options, you can use the Corsica.Router module:

defmodule MyApp.CORS do
  use Corsica.Router,
    origins: ["http://localhost", ~r{^https?://(.*\.)?foo\.com$}],
    allow_credentials: true,
    max_age: 600

  resource "/public/*", origins: "*"
  resource "/*"
end

defmodule MyApp.Endpoint do
  plug Logger
  plug MyApp.CORS
  plug MyApp.Router
end

This is only a brief overview of what Corsica can do. To find out more, head to the online documentation.

Common issues

Note that Corsica is compliant with the W3C CORS specification, which means CORS response headers are not sent for invalid CORS requests. The documentation goes into more detail about this, but it's worth noting so that the first impression is not that Corsica is doing nothing. One common pitfall is not including CORS request headers in your requests: this makes the request an invalid CORS request, so Corsica won't add any CORS response headers. Be sure to add at least the Origin header:

curl localhost:4000 -v -H "Origin: http://foo.com"

There is a dedicated page in the documentation that covers some of the common issues with CORS (and Corsica in part).

Contributing

If you find a bug, something unclear (including in the documentation!) or a behaviour that is not compliant with the latest revision of the official CORS specification, please open an issue on GitHub.

If you want to contribute to code or documentation, fork the repository and then open a Pull Request (how-to). Before opening a Pull Request, make sure all the tests passes by running $ mix test in your shell. If you're contributing to documentation, you can preview the generated documentation locally by running:

mix docs

Documentation will be generated in the doc/ directory.

License

MIT ยฉ 2015 Andrea Leopardi, see the license file.

More Repositories

1

vim-gotham

Code never sleeps in Gotham City. ๐ŸŒƒ
Vim Script
1,215
star
2

redix

Fast, pipelined, resilient Redis driver for Elixir. ๐Ÿ›
Elixir
1,062
star
3

stream_data

Data generation and property-based testing for Elixir. ๐Ÿ”ฎ
Elixir
843
star
4

xandra

Fast, simple, and robust Cassandra/ScyllaDB driver for Elixir.
Elixir
392
star
5

gotham-contrib

Ports of the Gotham colorscheme for common editors and terminal emulators ๐ŸŒƒ
Shell
242
star
6

guide_async_processing_in_elixir

A Livebook-based guide to async processing in Elixir. ๐Ÿƒ
127
star
7

vim-lengthmatters

Highlight the flooding part of an overly long line ๐Ÿ“
Vim Script
82
star
8

short_maps

โš ๏ธRetiredโš ๏ธ library that provided a ~m sigil for ES6-like map destructuring in Elixir.
Elixir
76
star
9

vim-textobj-xmlattr

A vim text object for XML/HTML attributes.
Vim Script
74
star
10

saul

Data validation and conformation library for Elixir.
Elixir
67
star
11

redix_pubsub

Deprecated Redis Pub/Sub client for Elixir. Now built-in into Redix.
Elixir
64
star
12

protohackers_in_elixir

Code for a video series I'm making on solving protohackers.com challenges in Elixir. ๐Ÿ“บ
Elixir
55
star
13

plug_heartbeat

A plug for responding to heartbeat requests.
Elixir
36
star
14

nimble_lz4

LZ4 compression for Elixir using Rust NIFs. ๐Ÿ—œ
Elixir
25
star
15

vim-textobj-erb

A Vim text object for ERB blocks.
Vim Script
20
star
16

dotfiles

My dotfiles ๐Ÿฅ‘
Shell
19
star
17

from_zero_to_hero_with_elixir

Elixir 101 training. Done by @ericmj and me.
Elixir
19
star
18

convertat

An Elixir library for converting from and to arbitrary bases.
Elixir
17
star
19

workshop-parallel-computation-with-elixir

Elixir
16
star
20

emacs.d

My Emacs config. โš™๏ธ (buuuuut I don't use Emacs anymore)
Emacs Lisp
15
star
21

training_otp_in_elixir

"OTP in Elixir" training done at Elixir Club Kyiv in ~2020. ๐Ÿ‘ฉโ€๐Ÿซ
Elixir
12
star
22

testing_aws_in_elixir

Support code for the blog post "Testing AWS in Elixir" ๐Ÿ•โ€๐Ÿฆบ
Elixir
12
star
23

small_ints

varint and ZigZag encoding/decoding for Erlang
Erlang
11
star
24

ecto_unix_timestamp

Ecto type for datetimes stored and cast as Unix timestamps. ๐Ÿ•ฐ๏ธ
Elixir
11
star
25

bases

A Ruby gem to convert from and to any base.
Ruby
10
star
26

concurrent_data_processing_in_elixir

Material for the "Concurrent Data Processing in Elixir" training.
10
star
27

connected-web-with-elixir-and-phoenix

Code for my Elixir/Phoenix workshop (currently at FunctionalConf India 2019).
Elixir
5
star
28

whatyouhide.github.io

My personal website. ๐Ÿ‘จโ€๐Ÿ’ป
SCSS
4
star
29

.vim

Vim configuration. Left to oblivion. โšฐ๏ธ
Vim Script
3
star
30

.atom

Atom configuration โš›๏ธ (buuut I don't use Atom anymore)
CSS
2
star
31

rack-domain

Rack middleware for dispatching to Rack apps based on the request domain.
Ruby
2
star
32

vim-tmux-syntax

Vim syntax for tmux configuration files.
Vim Script
2
star
33

advent_of_code_2022

Solutions to Advent of Code 2022, in Rust. ๐Ÿฆ€
Rust
1
star
34

intro-to-git-ingegneria

Git talk @ Univaq.
CSS
1
star
35

whatyouhide

Special repository for my GitHub homepage. ๐Ÿ›‹๏ธ
1
star