Search code examples
rake-pipeline

Proxy for ajax calls using rake-pipeline-web-filters


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?


Solution

  • 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.