Ruff9
Ruff9

Reputation: 1212

Sinatra app in Rails 4.1 config

I want to fake an API with a little Sinatra app, following this method. Meaning I have a Rails app, and in the spec/support folder, a very simple Sinatra app:

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

Part of the goal is to launch this app locally, so I can work on my Rails app with a fake API. Problem: when I do ruby spec/support/fake_programs.rb, the app can't boot, and I get a

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

Looks like Sinatra is booting using my Rails configuration. I don't need ActiveRecord nor Puma for my fake API.

I've read this question and this other one, but theese are in different context, because they need their Sinatra app to share routes with the Rails app.

Content of 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

I was looking at the config.ru file, but @max is right, it's not correlated.

Upvotes: 0

Views: 277

Answers (1)

matt
matt

Reputation: 79743

The Puma README says:

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

Which is why your app is using this config file.

It goes on to say:

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 "-"

Since Puma can handle Sinatra apps directly, you could start your app like this:

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

If you want to start your app using ruby with Puma as the built in server (as you are doing now), I think adding this to your Sinatra app should work (neither Sinatra’s :server_settings or Puma’s :config_files are very well documented):

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

Upvotes: 2

Related Questions