Passing options to rackup via a Sinatra application
Asked Answered
W

2

13

I'm new to ruby, learning Sinatra. While creating a Sinatra site by requiring 'sinatra' and setting up the routes directly under is pretty easy and rather well documented, creating an application by requiring 'sinatra/base' and writing a class that inherits from 'Sinatra::Base', while still relatively easy, is very poorly documented (maybe because it's a pretty recent feature of Sinatra).

And that's exactly what I am doing. I am not having too much trouble on the Sinatra part, however I am having a bit of trouble on the rackup/thin/server part. Apparently there are two ways to deploy the application: using Sinatra itself (using the run! method) and using a rackup file (typically config.ru).

Using Sinatra's run! method is extremely intuitive and works like a charm, but apparently it doesn't work if I want to deploy my app on heroku. As a matter of fact, almost all the Sinatra apps that I have encountered on GitHub use a config.ru file.

Using a rackup file might be equally intuitive, but I can't manage to understand how to pass options from the Sinatra app to the server (ir: the port). I tried to merge options to rackup's default options array:

MyApp::App.default_options.merge!(
  :run  => false,
  :env  => :production,
  :port => 4567
)

run MyApp::App

by adding options directly to the app:

MyApp::App.set :port, 4567
MyApp::App.set :run, false
MyApp::App.set :env, :production

run MyApp::App

by setting options from within the application class:

module MyApp
  class App < Sinatra::Base
    set :port, 4567
    set :run, false
    set :env, :production

    # ...

  # config.ru
  require 'app'

  run MyApp::App

All the methods above failed, either by showing error messages or by just not taking any of the options into consideration. So is there any way to pass options to rackup/thin/the sever via a Sinatra app when using a rackup file? Or the options in questions should be passed directly to rackup/thin/the sever via command-line options?

As a reference to the problem, here is the little Sinatra application I am building: https://github.com/AzizLight/Wiki/

Wrangle answered 8/12, 2010 at 15:29 Comment(0)
B
11

You're actully going to pass options to thin on the command line directly or via a configuration file. See all options:

$ thin -h

For production, use a configuration file:

$ thin -C thin-production.yml -R config.ru start

Here is an example thin-production.yml file:

---
address: localhost
port: 3020
servers: 4
max_conns: 1024
max_persistent_conns: 512
timeout: 30
environment: production
pid: tmp/pids/thin-production.pid
log: log/thin-production.log
daemonize: true 
Brunildabruning answered 14/12, 2010 at 3:45 Comment(0)
H
3

I know I'm resurrecting an ancient question here, but I came across another useful solution not yet mentioned. As stated in this rack wiki tutorial:

the first line starting with #\ is treated as if it was options, allowing rackup arguments to be specified in the config file.

So if you wanted to set your host to 0.0.0.0 and port to 5656, you would add the following line to the beginning of your config.ru file:

#\ -o 0.0.0.0 -p 5656
Hemidemisemiquaver answered 1/5, 2016 at 18:34 Comment(2)
Works perfectly. But is there a way to set values from code? for example :port => RACK_ENV=="production" ? 8080 : 3000Aright
Perhaps enclosing '#\' comment setting lines within 'if else'? Not sure thoughHemidemisemiquaver

© 2022 - 2024 — McMap. All rights reserved.