To add CORS headers to a HTTP server with a specific origin and methods in Ruby, you can use the rack-cors middleware.
Here's an example of how to use it:
main.rb289 chars16 lines
In this example, we first require the rack/cors module. We then define the allowed origins and methods as arrays.
Next, we create the middleware using Rack::Cors, and specify the allow option. We use the origins method to set the allowed origins, and methods to set the allowed methods.
Finally, we run our app, which can be any Rack-compatible app.
With this middleware, requests with an origin not in allowed_origins or a method not in allowed_methods will be rejected with a CORS error.
gistlibby LogSnag