Rails 4.1 配置中的 Sinatra 应用程序

Sinatra app in Rails 4.1 config

我想在 this method 之后用一个小的 Sinatra 应用程序伪造一个 API。这意味着我有一个 Rails 应用程序,在 spec/support 文件夹中,有一个非常简单的 Sinatra 应用程序:

module FakePrograms
  class Application < Sinatra::Base
    get "/API/V1/programs" do
      {
        programs: [
          ...
        ]
      }.to_json
    end
  end
end

部分目标是在本地启动此应用程序,这样我就可以使用假 API 开发我的 Rails 应用程序。问题:当我执行 ruby spec/support/fake_programs.rb 时,应用程序无法启动,我得到一个

config/puma.rb:14:in `block in _load_from':
uninitialized constant
#<Class:#<Puma::DSL:0x007fac0b0e0380>>::ActiveRecord (NameError)

看起来 Sinatra 正在使用我的 Rails 配置启动。我的假 API.

不需要 ActiveRecord 或 Puma

我读过 this question and this other one,但是这些内容不同,因为他们需要他们的 Sinatra 应用程序与 Rails 应用程序共享路线。

config/puma.rb 的内容:

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end

我正在查看 config.ru 文件,但@max 是正确的,它不相关。

Puma README 说:

By default, if no configuration file is specified, Puma will look for a configuration file at config/puma.rb.

这就是您的应用使用此配置文件的原因。

接着说:

If you want to prevent Puma from looking for a configuration file in those locations, provide a dash as the argument to the -C (or --config) flag:

$ puma -C "-"

由于 Puma 可以直接处理 Sinatra 应用程序,您可以这样启动您的应用程序:

$ puma -c "-" spec/support/fake_programs.rb

如果你想使用 ruby 启动你的应用程序并将 Puma 作为内置服务器(就像你现在所做的那样),我认为将它添加到你的 Sinatra 应用程序应该可行(Sinatra 的 :server_settings 或 Puma 的 :config_files 有很好的记录):

set :server, 'puma'
set :server_settings, {:config_files => "-"}