Class: Whoosh::Middleware::Cors

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/middleware/cors.rb

Constant Summary collapse

DEFAULT_METHODS =
"GET, POST, PUT, PATCH, DELETE, OPTIONS"
DEFAULT_HEADERS =
"Content-Type, Authorization, X-API-Key, X-Request-ID"

Instance Method Summary collapse

Constructor Details

#initialize(app, origins: ["*"], methods: DEFAULT_METHODS, headers: DEFAULT_HEADERS, max_age: 86_400) ⇒ Cors

Returns a new instance of Cors.



9
10
11
12
13
14
15
# File 'lib/whoosh/middleware/cors.rb', line 9

def initialize(app, origins: ["*"], methods: DEFAULT_METHODS, headers: DEFAULT_HEADERS, max_age: 86_400)
  @app = app
  @origins = origins
  @methods = methods
  @headers = headers
  @max_age = max_age
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/whoosh/middleware/cors.rb', line 17

def call(env)
  origin = env["HTTP_ORIGIN"]

  if env["REQUEST_METHOD"] == "OPTIONS"
    return preflight_response(origin)
  end

  status, headers, body = @app.call(env)
  add_cors_headers(headers, origin)
  [status, headers, body]
end