Corsica v1.1.3 Corsica.Router View Source
A router to handle and respond to CORS requests.
This module provides facilities for creating
Plug.Router-based routers that
handle CORS requests. A generated router will handle a CORS request by:
- responding to it if it's a preflight request (refer to
Corsica.send_preflight_resp/4for more information) or
- adding the right CORS headers to the
Plug.Connconnection if it's a valid CORS request.
When a module calls
use Corsica.Router, it can pass the same options that
can be passed to the
Corsica plug to the
use call. Look at the
documentation for the
Corsica module for more information about these
defmodule MyApp.CORS do use Corsica.Router, origins: ["http://foo.com", "http://bar.com"], allow_credentials: true, max_age: 600 resource "/*" # We can override single settings as well. resource "/public/*", allow_credentials: false end
Now in your application's endpoint:
defmodule MyApp.Endpoint do plug Plug.Head plug MyApp.CORS plug MyApp.Router end
Note that a
Corsica.Router router will always define a match-all route after
resource routes; this match-all route will simply return the connection
unchanged, effectively continuing with the plug pipeline.
Link to this section Summary
Link to this section Functions
Defines a CORS-enabled resource.
This macro takes advantage of the macros defined by
match/3) in order to define regular
Plug.Router-like routes that
efficiently match on the request url; the bodies of the autogenerated routes
just perform a couple of checks before calling either
Note that if the request is a CORS preflight request (whether it's a valid one
or not), a response is immediately sent to the client (whether the request is
a valid one or not). This behaviour, combined with the definition of an
OPTIONS route to
Corsica.Router ideal to just
put before any router in a plug pipeline, letting it handle preflight requests
The options given to
resource/2 are merged with the default options like it
happens with the rest of the functions in the
Corsica module. The
macro also accepts the following options (similar to
:host- the host which the route should match. Defaults to
nil, meaning no host match, but can be a string like
"example.com"or a string ending with
"subdomain.", for a subdomain match.
resource "/foo", origins: "*" resource "/wildcards/are/ok/*", max_age: 600 resource "/only/on/subdomain", host: "mysubdomain."