2

I'm setting up my devel environment for an Ember.js app using rake-pipeline as described here.

During development, my html and javascript are served by webrick (rake-filter magic that I don't quite understand) on http://0.0.0.0:9292 and I have a REST service developed in php served by Apache on http://somename.local

My ajax calls from the client app are getting lost because of the browser's anti-cross-domain-ajax thing. How do I work around this issue?

Community
  • 1
  • 1
Rafael Vega
  • 4,575
  • 4
  • 32
  • 50

2 Answers2

2

You can't configure the proxy directly in your Assetfile. You'll have to create a config.ru file and use the rackup command to launch the server.

Here's an example Assetfile:

input "app"
output "public"

And config.ru:

require 'rake-pipeline'
require 'rake-pipeline/middleware'
require "rack/streaming_proxy" # Don't forget to install the rack-streaming-proxy gem.

use Rack::StreamingProxy do |request|
  # Insert your own logic here
  if request.path.start_with?("/api")
    "http://localhost#{request.path.sub("/api", "")}"
  end
end

use Rake::Pipeline::Middleware, 'Assetfile' # This is the path to your Assetfile
run Rack::Directory.new('public') # This should match whatever your Assetfile's output directory is

You'll have to install the rack and rack-streaming-proxy gems.

Rafael Vega
  • 4,575
  • 4
  • 32
  • 50
ebryn
  • 4,407
  • 1
  • 23
  • 21
1

You can use Rack::Proxy and then just send the needed requests to the proxy.

  if request.path.start_with?("/api")
    URI.parse("http://localhost:80#{request.path}")
  end
Thomas Bartelmess
  • 1,111
  • 8
  • 17
  • Thanks Thomas, could you elaborate a bit more? I have an Assetfile that looks like this https://gist.github.com/1608930 Where would I add the Rack::Proxy code? – Rafael Vega Jan 13 '12 at 21:59
  • And how does this transfer the method, POST, DELETE, or PUT? It looks like you're just converting every method into a GET. – Lucas Sep 06 '12 at 22:20