使用机架安装的多个 Sinatra 应用程序

问题描述:

我有一个关于在 Sinatra 中使用机架安装的问题.我有两个经典风格的 Sinatra 应用程序.让我们调用 app.rb 中定义的一个 App 和 api.rb 中定义的另一个 API.

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

我希望 api.rb 处理所有以 '/api' 开头的路由,而 app.rb 处理所有其他请求,包括根 ('/').

I would like it so that api.rb handles all routes beginning with '/api' and app.rb handles all other requests including the root ('/').

我将如何通过机架安装进行设置?或者有比这更好的解决方案吗?

How would I set this up with rack-mount? Or is there a better solution than that?

我想你会更喜欢 Rack::URLMap - 它可能看起来像这样:

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

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

这应该在您的 config.ru 文件中.

That should go in your config.ru file.