linuxdan
linuxdan

Reputation: 4854

How do I make my rack app a gem?

I have a nice litte .ru file that I can run with rackup but I want to be able to publish it as as a gem. I assume I can move it to the lib directory and add it to my gemspec but what else do I need to do so that I can run just run it after installing the gem?

Upvotes: 1

Views: 407

Answers (2)

linuxdan
linuxdan

Reputation: 4854

Here's what I ended up with:

#!/usr/bin/env ruby

require 'rack'
require 'illusionist'

options = {
  :Host => '127.0.0.1',
  :Port => '8080'
}

merlin = Illusionist.new

Rack::Handler::Thin.run(merlin, options) do |server|
  [:INT, :TERM].each { |sig| trap(sig) { server.stop } }
end

I renamed my .ru file to .rb and then launched it with the above code. Thank you @Anton for getting me started.

Upvotes: 0

Anton
Anton

Reputation: 3036

Gemspec+correct directory structure+(most importantly) placing a script that will launch your app(with run, probably) into bin/ directory.

A little more details on gem binaries here

UPDATE

An example as requested. I have made a gem called agent which depends on sinatra(it also depends on rack). It has this definition of Agent::Server:

module Agent
  # Your code goes here...
  class Server <  ::Sinatra::Base

    get '/sync' do
        [200, "yahoo!"]
    end
  end

I also created file called test with following contents:

#!/usr/bin/env ruby

require "rubygems"
require "agent"

Rack::Handler::WEBrick.run(
  Agent::Server.new,
  :Port => 9000
)

Then, if I run chmod 0755 test and ./test after that, I can go to http://localhost:900/sync and see yahoo!.

Upvotes: 3

Related Questions