7

I've a very basic test app. When I execute this command the server ignores the port I specify and runs Thin on port 4567. Why is the port I specify ignored?

$ruby xxx.rb start -p 8000

== Sinatra/1.3.3 has taken the stage on 4567 for production with backup from Thin
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop

xxx.rb file

require 'Thin'

rackup_file = "config.ru" 

argv = ARGV
argv << ["-R", rackup_file ] unless ARGV.include?("-R")
argv << ["-e", "production"] unless ARGV.include?("-e")

puts argv.flatten

Thin::Runner.new(argv.flatten).run!

config.ru file

require 'sinatra'
require 'sinatra/base'

class SingingRain < Sinatra::Base
    get '/' do
        return 'hello'
    end
end

SingingRain.run!
Roman
  • 10,309
  • 17
  • 66
  • 101

3 Answers3

16
#\ -p 8000

put this at the top of the config.ru

James
  • 1,841
  • 1
  • 18
  • 23
14

Your problem is with the line:

SingingRain.run!

This is Sinatra’s run method, which tells Sinatra to start its own web server which runs on port 4567 by default. This is in your config.ru file, but config.ru is just Ruby, so this line is run as if it was in any other .rb file. This is why you see Sinatra start up on that port.

When you stop this server with CTRL-C, Thin will try to continue loading the config.ru file to determine what app to run. You don’t actually specify an app in your config.ru, so you’ll see something like:

^C>> Stopping ...

== Sinatra has ended his set (crowd applauds)
/Users/matt/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:129:in `to_app': missing run or map statement (RuntimeError)
        from config.ru:1:in `<main>'
        ...

This error is simply telling you that you didn’t actually specify an app to run in your config file.

Instead of SingingRain.run!, use:

run SingingRain

run is a Rack method that specifies which app to run. You could also do run SingingRain.new – Sinatra takes steps to enable you to use just the class itself here, or an instance.

The output to this should now just be:

>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:8000, CTRL+C to stop

You don’t get the == Sinatra/1.3.2 has taken the stage on 4567 for production with backup from Thin message because Sinatra isn’t running its built in server, it’s just your Thin server as you configured it.

matt
  • 78,533
  • 8
  • 163
  • 197
  • It's really hard sometimes to get good answers like yours. Thank you a world :) – Roman Sep 26 '12 at 00:29
  • Your answer worked for me but I have another question. Now when I run the `$ ruby thinx.rb start -d` command I get two `exit` outputs even though the server starts properly. Do you know what's that about? – Roman Sep 26 '12 at 01:14
  • @Arman sorry, I don’t know what’s happening with your two outputs – it doesn’t happen for me. – matt Sep 27 '12 at 20:29
-3

in your config.ru add

set :port=> 8000

Also i would highly suggest using Sinatra with something like passenger+nginx which makes deploying to production a breeze. But You need not worry about this if you are going to deploy to heroku.

djd
  • 1,007
  • 1
  • 7
  • 20
  • Doesn't Thin:Runner constructor specifically parse command line arguments as documented here? https://github.com/macournoyer/thin/blob/master/lib/thin/runner.rb – Roman Sep 24 '12 at 23:32
  • that looks like it but iam not really sure how that works out when u are using a config.ru file. If you had not created a config.ru file and it was just .rb thin does accept the -p parameter – djd Sep 25 '12 at 06:10