Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Thin run on a different port?

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!
like image 487
Roman Avatar asked Sep 24 '12 05:09

Roman


2 Answers

#\ -p 8000

put this at the top of the config.ru

like image 188
James Avatar answered Oct 14 '22 05:10

James


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.

like image 22
matt Avatar answered Oct 14 '22 03:10

matt