所以我用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.
但是,当我尝试将其打包为宝石时:
#!/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
我可以安装好的宝石:
% 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...
但是运行已安装的可执行文件无法启动Web服务器
% which sinatra_demo
~/.rvm/gems/ruby-1.9.2-p180/bin/sinatra_demo
% sinatra_demo
%
gem的可执行文件正在运行(我可以通过puts
语句告诉我),但是web服务器无法启动。
答案 0 :(得分:5)
您需要添加
enable :run
到您的应用程序文件,以便内置的Web服务器启动。
来自Sinatra configuration settings:
默认情况下,仅当:app_file与$ 0匹配时才启用此设置。即,直接使用ruby myapp.rb运行Sinatra应用程序文件。
直接运行文件时,此条件为true,因此Web服务器启动。但是,当打包为gem时,实际运行的可执行文件实际上是围绕由rubygems创建的应用程序文件的包装脚本,因此条件为false且Web服务器无法启动。
答案 1 :(得分:0)
以防万一其他人到此为止,也有可能app.rb不在您认为的文件夹中。