How to ship a Sinatra application as a gem and deploy it?

痴心易碎 提交于 2019-12-13 11:50:28

问题


I have a sinatra application and packaged that as a gem. Its file-layout looks roughly like this:

├── bin
│   └── tubemp
├── lib
│  └── tubemp.rb
├── Gemfile
└── tubemp.gemspec

I can install and run it just fine. Calling ruby lib/tubemp.rb fires the app too, because Sinatra made it self-starting. tubemp.rb:

class Tubemp < Sinatra::Application
  get '/' do
    erb :index, :locals => { :title => "YouTube embeds without third party trackers." }
  end
end

The binary is really simple too. bin/tubemp:

#!/usr/bin/env ruby
require "tubemp.rb"
Tubemp.run!

But now I want to deploy this as Rack-app. Or deploy it inside a Rack-app. At least it should run under Passenger on a production machine.

With a generic application it is as simple as adding a config.ru in the directory where the application lives. This file then, roughly, includes and calls run Tubemp. Pointing nginx or apache's passenger at the dir where this rackup file and the app lives, starts it. This worked, up to the point where I made it a gem; because now I no longer have "a directory where the app lives", other then where gem install tubemp decides to place the files.

Do I need to create a wrapper application wich bundles the tubemp-gem and its dependencies? If so, how do I call the gem from a rackup file? Or am I going at this entirely wrong?


回答1:


Ignoring path-issues for now, here is how I solved it:

The application bundled as gem, tubemp.

A Gemfile to install and include the gemified application:

gem 'tubemp'

A config.ru which runs the gemified application, called Tubemp:

require 'rubygems'
require 'bundler/setup' # To allow inclusion via the Bundle/Gemfile
require 'sinatra'       # Sinatra is required so we can call its "set"
require 'tubemp'        # And include the application

# Set the environment to :production on production
set :environment, ENV['RACK_ENV'].to_sym

# And fire the application.
run Tubemp

These two files is all that is needed to make a new rack-application, which includes the gemified Sinatra-app and as such are bootable trough e.g. nginx, passenger or simply rackup.



来源:https://stackoverflow.com/questions/16100398/how-to-ship-a-sinatra-application-as-a-gem-and-deploy-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!