Last active
August 16, 2017 20:33
-
-
Save richhollis/b98a8b0599860145ad86 to your computer and use it in GitHub Desktop.
CorsMiddleware which can be useful for testing SwaggerUI resources in development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CorsMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
default_headers = { | |
'Access-Control-Allow-Origin' => '*', | |
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, PUT, PATCH, OPTIONS', | |
'Access-Control-Allow-Headers' => 'Content-Type, api_key, Authorization, origin' | |
} | |
if env["REQUEST_METHOD"] == "OPTIONS" | |
puts "CorsMiddleware: Intercepted OPTIONS request: #{env["REQUEST_URI"]}" | |
return [200, default_headers, ["<html><body><h1>Intercepted</h1><p>This <b>#{env["REQUEST_METHOD"]}</b> request was intercepted by the CorsMiddleware</p></body></html>"]] | |
end | |
status, headers, body = @app.call(env) | |
[status, headers.merge(default_headers), body] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Getaroom::Application.configure do | |
# Settings specified here will take precedence over those in config/application.rb | |
# | |
# ... | |
# | |
# Middleware for settings CORS headers (for dev testing API with swagger UI | |
config.middleware.insert_before ActionDispatch::Static, CorsMiddleware | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment