Thermatix Thermatix - 1 year ago 106
Ruby Question

set server options inside rackup file

I'm trying set the server options I.E. port, host, etc but I can't find anything on how to do this from within the

I've tried putting the config options into a hash and then doing:

configure { set :server, config[:server][:handler].to_sym }, config[:server])

Also tried:

Rack::Handler::pick(['puma']).run App, config[:server]

and even:

configure { set :server, config[:server].delete(:handler).to_sym }

so that the handler won't be in the server config hash and still...

no dice.

config hash is:

:handler => "puma",
:host => "",
:port => 3000,
:threads => "0:16",
:verbose => true

But the hash config just gets ignored, I set the port to
but the app loads with
as default.

It also errors about there not being a
command present (well obviously, I'm not using it).

So a fix for that would also be a nice.

I'm sure there's a proper way to do this but why is it so hard to find it documented? I've done as many search terms into google as I can think of and yet nothing completely correct comes back.

mwp mwp
Answer Source

It's not documented well because most people don't do what you're trying to do. :-) Folks typically store their Puma configuration in config/puma.rb or pass it on the command line e.g. in Procfile.

I'm going to out on a limb here and assume your App is a Sinatra app or something similar. The main issue with trying to set these options in a Sinatra configure {} block is that by the time rackup is running the class and executing these statements it's already too late to set things like the port and thread pool size. As far as the missing run method go, I think you just want run App in Not sure what you're going for there.

You can tell rackup to use Puma by adding this at the top of the file:

#\ -s Puma

If you want to set the port or any other rackup options, you can do it like so:

#\ -s Puma -p 3000

or, for Puma-specific options:

#\ -s Puma -p 3000 -O Threads=0:16 -O Verbose=true

This is (mostly) documented in Puma's README here, and here.

Another option is to skip rackup and entirely and just build everything into your Sinatra app:

require 'sinatra/base'
require 'puma'

class App < Sinatra::Application
  configure do
    set :server, :puma
    set :port, 3000
    set :server_settings, :Threads => '0:16', :Verbose => true

  run! if $0 == app_file

Then you can just run your app like any normal Ruby script, e.g. ruby app.rb.

At the end of the day, I would strongly recommend you explore creating a Puma configuration file and using that instead. It's just easier, cleaner, and more understandable. If you need to pull in the Puma settings from the environment or from the result of another method or process, you can do that in there. Best of luck.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download