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 config.ru file.
I've tried putting the config options into a hash and then doing:
configure { set :server, config[:server][:handler].to_sym }
Rack::Handler.default.run(App, 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 => "127.0.0.1",
:port => 3000,
:threads => "0:16",
:verbose => true
}
But the hash config just gets ignored, I set the port to 3000 but the app loads with 8080 as default.
It also errors about there not being a run 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.
You can specify options on a line staring with #\ in config.ru (it must be the first such line). You specify them as if you were specifying command line options to rackup:
#\ -s puma -o 127.0.0.1 -p 3000 -O Threads=0:16 -O Verbose
# require everything and set up middleware etc.
run MyApp
The docs for this are hidden away on the wiki.
Check rackup -s puma -h for the options you can use. -O passes the option through to the server you are using (Puma seems to accept Threads and Verbose).
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 goes, I think you just want run App in config.ru. 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 config.ru 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
end
run! if $0 == app_file
end
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With