[英]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. 我有一个很好的豆蔻.ru
,我可以运行文件rackup
,但我希望能够将其发布为一个宝石。 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? 我假设我可以将其移动到lib
目录并将其添加到我的gemspec
但是我还需要做些什么,以便我可以在安装gem之后运行它呢?
Gemspec
+correct directory structure+(most importantly) placing a script that will launch your app(with run
, probably) into bin/
directory. Gemspec
+正确的目录结构+(最重要的是)将一个脚本启动,将启动您的应用程序(可能带有run
)到bin/
目录中。
A little more details on gem binaries here 这里有更多关于宝石二进制文件的细节
UPDATE UPDATE
An example as requested. 要求的示例。 I have made a gem called agent
which depends on sinatra
(it also depends on rack
). 我已经制作了一个名为agent
的宝石,它取决于sinatra
(它也取决于rack
)。 It has this definition of Agent::Server
: 它具有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: 我还创建了一个名为test
文件,其内容如下:
#!/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!
然后,如果我运行chmod 0755 test
和./test
之后,我可以转到http://localhost:900/sync
并查看yahoo!
. 。
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. 我将.ru文件重命名为.rb,然后使用上面的代码启动了它。 Thank you @Anton for getting me started. 谢谢@Anton让我入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.