How to make Thin run on a different port?
Asked Answered
C

3

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!
Clance answered 24/9, 2012 at 5:49 Comment(0)
T
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.

Tartlet answered 25/9, 2012 at 11:21 Comment(3)
It's really hard sometimes to get good answers like yours. Thank you a world :)Clance
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?Clance
@Arman sorry, I don’t know what’s happening with your two outputs – it doesn’t happen for me.Tartlet
N
16
#\ -p 8000

put this at the top of the config.ru

Noisome answered 24/9, 2013 at 3:1 Comment(1)
This worked for Sinatra/Thin by running command "rackup." Perfect!Brandonbrandt
T
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.

Tartlet answered 25/9, 2012 at 11:21 Comment(3)
It's really hard sometimes to get good answers like yours. Thank you a world :)Clance
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?Clance
@Arman sorry, I don’t know what’s happening with your two outputs – it doesn’t happen for me.Tartlet
C
-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.

Cheops answered 24/9, 2012 at 6:14 Comment(2)
Doesn't Thin:Runner constructor specifically parse command line arguments as documented here? github.com/macournoyer/thin/blob/master/lib/thin/runner.rbClance
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 parameterCheops

© 2022 - 2024 — McMap. All rights reserved.