Multiple Sinatra apps using rack-mount

前端 未结 4 1749
南方客
南方客 2020-12-13 20:47

I have a question regarding using rack-mount with Sinatra. I\'ve got two classic-style Sinatra apps. Let\'s call one App defined in app.rb and the other API defined in api.r

相关标签:
4条回答
  • 2020-12-13 21:00

    I had the same problem once and so I came up with this template: sinatra-rspec-bundler-template which is layed out for multiple apps.

    It may have more features than you need but it should help you when you need something "a bit more" complex.

    0 讨论(0)
  • I had a similar issue and I am not very familiar with Rack. I could not figure out what to do based on the answers above. My final solution was to have the following in config.ru.

    This works perfectly for me.

    # Main Ramaze site
    map "/" do
    
        Encoding.default_external = Encoding::UTF_8
        Encoding.default_internal = Encoding::UTF_8
    
        require ::File.expand_path('../app', __FILE__)
    
        Ramaze.start(:root => __DIR__, :started => true)
        run Ramaze
    
    end
    
    # Sinatra & Grape API
    map "/api" do
    
        use Rack::Static, :urls => ["/stylesheets", "/images", "/javascripts"], :root => "public"
        use Rack::Session::Cookie
    
        run Rack::Cascade.new([
            MySinatraApp::Application,
            MySinatraApp::API])
    
    end
    
    0 讨论(0)
  • 2020-12-13 21:10

    I think you'll prefer Rack::URLMap - it will probably look something like this:

    run Rack::URLMap.new("/" => App.new, 
                         "/api" => Api.new)
    

    That should go in your config.ru file.

    0 讨论(0)
  • 2020-12-13 21:20

    In config.ru you could also take advantage of Sinatra's middleware feature. If you have several Sinatra apps, each with its own routes, and want to run them simultaneously, you can arrange them in the order you want them found, e.g.

    # config.ru
    ...
    use MyAppA
    use MyAppB
    use MyAppC
    run MyAppD
    
    0 讨论(0)
提交回复
热议问题