Search code examples
rubyunicornthinpuma

How to start a minimal ruby app using Puma or Unicorn?


I have a very basic ruby example running on Thin, but I would like to know how to translate this example to use Unicorn or Puma as the HTTP server instead. Here is the code I have now:

require 'rack'

class HelloWorld
  def talk()
    return "Hello World!"
  end
end

class SomeServer
  def start(server_object, port)
    app = proc do |env|
      [ 200, {"Content-Type" => "text/html"}, [server_object.talk()] ]
    end

    Rack::Handler::Thin::run(app, :Port => port)
  end
end

SomeServer.new.start(HelloWorld.new, 3000)

This runs fine and well, but I cannot figure out how to make it run using Puma or Unicorn instead. Most online documentation I find for the two is for Rails apps. How can I utilize the multi-threading capabilities of these servers with this simple program?


Solution

  • use sinatra.

    So to take it step by step first install sinatra and puma gems

    gem install sinatra
    
    gem install puma
    

    then create a file myapp.rb

    require 'sinatra'
    configure { set :server, :puma }
    
    get '/' do
      "Hello World!"
    end
    

    then run the file

    ruby myapp.rb
    

    by default sinatra listens on 4567 so go to localhost:4567 you can configure puma to listen on a specific port or do a lot of other things using a config file read the documentation