简体   繁体   中英

How do I mount a standalone Rack app into a Ruby On Rails app?

I have a running Rails app. Then, I have a little script that I would like to mount to the Rails app - here's the script :

require 'as2'
require 'rack'

As2.configure do |conf|
  ...
end

handler = As2::Server.new do |filename, body|
  ...
end

MyBuilder = Rack::Builder.new do
  use Rack::CommonLogger
  map '/as2' do
    run handler
  end
end

puts "As2 version: #{As2::VERSION}"

I was researching how to mount the Rack app to the Rails app, and I should do something like this:

MyRailsApp::Application.routes.draw do
  mount MySinatraApp.new => '/api'
end

However, what I struggle with - what's the identificator (ID) of the Rack app? How do I set it and how do I connect these two apps and successfully mount them together?

Thank you in advance.

EDIT: When I place the file (of which snippet I posted above) place to the /lib folder - the file's name as2_server.rb and to the routes I put the following:

Rails.application.routes.draw do
  ...
  mount MyBuilder.new => '/as2'
end

I get the following error:

uninitialized constant MyBuilder (NameError)

How (where) should I properly register the MyBuilder rack app?

I think the only piece you're missing is requiring your Rack application.

So, to summarize, I would:

  • put your script in lib/my_builder.rb
  • create config/initializers/my_builder.rb
  • add require Rails.root.join('lib/my_builder') to the new initializer
  • mount with mount MyBuilder, at: "/my_builder" in config/routes.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM