gem executable that starts sinatra server not working
So I wrote a simple "Hello World" site using sinatra:
#!/usr/bin/env ruby
# sinatra_demo/bin/sinatra_demo
require 'rubygems'
require 'sinatra'
get "/hello" do
"Hello World!"
end
And when I run it, it works, I can send my browser to http://localhost:4567/hello and get "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.
However, when I try to package it as a gem:
#!/usr/bin/env gem build
# sinatra_demo/sinatra_demo.gemspec
require 'rubyg开发者_C百科ems'
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
I can install the gem fine:
% 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...
But running the installed executable fails to start the webserver
% which sinatra_demo
~/.rvm/gems/ruby-1.9.2-p180/bin/sinatra_demo
% sinatra_demo
%
The gem's executable is getting run (from what I can tell via puts
statements), but the webserver doesn't start.
What am I doing wrong? (gist for the files if you want to play with them)
You need to add
enable :run
to your application file, so that the built in web server will start.
From the 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.
When running the file directly this condition is true so the web server starts. When packaged as a gem however, the executable file that is actually run is really a wrapper script around your application file created by rubygems, so the condition is false and the web server doesn't start.
Just in case anyone else ends up here, there is also the possibility that app.rb is not in the folder you think it is.
精彩评论