gpt4 book ai didi

ruby - 启动 sinatra 服务器的 gem 可执行文件不工作

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:05 24 4
gpt4 key购买 nike

所以我使用 sinatra 编写了一个简单的“Hello World”站点:

#!/usr/bin/env ruby

# sinatra_demo/bin/sinatra_demo

require 'rubygems'
require 'sinatra'

get "/hello" do
"Hello World!"
end

当我运行它时,它起作用了,我可以将我的浏览器发送到 http://localhost:4567/hello 并获得“Hello World”:

% sinatra_demo/bin/sinatra_demo
== Sinatra/1.2.6 has taken the stage on 4567 for development with backup from WEBrick
[2011-06-30 09:29:58] INFO WEBrick 1.3.1
[2011-06-30 09:29:58] INFO ruby 1.9.2 (2011-02-18) [x86_64-darwin10.7.4]
[2011-06-30 09:29:58] INFO WEBrick::HTTPServer#start: pid=73620 port=4567
127.0.0.1 - - [30/Jun/2011 09:30:10] "GET /hello HTTP/1.1" 200 12 0.0027
localhost - - [30/Jun/2011:09:30:10 EDT] "GET /hello HTTP/1.1" 200 12
- -> /hello
127.0.0.1 - - [30/Jun/2011 09:30:10] "GET /favicon.ico HTTP/1.1" 404 447 0.0004
localhost - - [30/Jun/2011:09:30:10 EDT] "GET /favicon.ico HTTP/1.1" 404 447
- -> /favicon.ico
127.0.0.1 - - [30/Jun/2011 09:30:10] "GET /favicon.ico HTTP/1.1" 404 447 0.0003
localhost - - [30/Jun/2011:09:30:10 EDT] "GET /favicon.ico HTTP/1.1" 404 447
- -> /favicon.ico
^C
== Sinatra has ended his set (crowd applauds)
[2011-06-30 09:30:12] INFO going to shutdown ...
[2011-06-30 09:30:12] INFO WEBrick::HTTPServer#start done.

但是,当我尝试将其打包为 gem 时:

#!/usr/bin/env gem build

# sinatra_demo/sinatra_demo.gemspec

require 'rubygems'

Gem::Specification.new do |spec|
spec.name = 'sinatra_demo'
spec.summary = "A hello world webserver"
spec.author = "rampion"
spec.files = Dir['bin/*']
spec.executables = ['sinatra_demo']
spec.version = "1.0.0"
spec.add_dependency('sinatra')
spec.has_rdoc = false
end

我可以很好地安装 gem:

% sinatra_demo/sinatra_demo.gemspec
WARNING: no description specified
WARNING: no email specified
WARNING: no homepage specified
Successfully built RubyGem
Name: sinatra_demo
Version: 1.0.0
File: sinatra_demo-1.0.0.gem
% gem install sinatra_demo-1.0.0.gem
Successfully installed sinatra_demo-1.0.0
1 gem installed
Installing ri documentation for sinatra_demo-1.0.0...
Installing RDoc documentation for sinatra_demo-1.0.0...

但是运行已安装的可执行文件无法启动网络服务器

% which sinatra_demo
~/.rvm/gems/ruby-1.9.2-p180/bin/sinatra_demo
% sinatra_demo
%

gem 的可执行文件正在运行(我可以通过 puts 语句得知),但网络服务器没有启动。

我做错了什么? ( gist for the files if you want to play with them )

最佳答案

你需要添加

enable :run

到你的应用程序文件,这样内置的网络服务器就会启动。

来自Sinatra configuration settings :

By default, this setting is enabled only when the :app_file matches $0. i.e., when running a Sinatra app file directly with ruby myapp.rb.

当直接运行文件时,此条件为真,因此 Web 服务器启动。但是,当打包为 gem 时,实际运行的可执行文件实际上是由 ruby​​gems 创建的围绕您的应用程序文件的包装器脚本,因此条件为 false,Web 服务器不会启动。

关于ruby - 启动 sinatra 服务器的 gem 可执行文件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6535646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com