Sinatra在ctrl-c之后重新启动Webrick Server

时间:2013-04-19 20:17:42

标签: ruby sinatra webrick

require 'sinatra'
require 'rubygems'

class TestServer < Sinatra::Application
set :port, 22340
    get '/' do
        "Hello World"
    end
    run! if app_file == $0
end

Ruby 2.0.0-p0和Sinatra 1.4.2的非常简单的应用程序

当我按下ctrl-c时,webrick服务器会在默认端口上重新启动...请参阅

下面的输出
LM-BOS-00715009:server joshughes$ ruby test.rb 
[2013-04-19 16:07:48] INFO  WEBrick 1.3.1
[2013-04-19 16:07:48] INFO  ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2]
== Sinatra/1.4.2 has taken the stage on 22340 for development with backup from WEBrick
[2013-04-19 16:07:48] INFO  WEBrick::HTTPServer#start: pid=63798 port=22340
^C
== Sinatra has ended his set (crowd applauds)
[2013-04-19 16:07:56] INFO  going to shutdown ...
[2013-04-19 16:07:56] INFO  WEBrick::HTTPServer#start done.
[2013-04-19 16:07:56] INFO  WEBrick 1.3.1
[2013-04-19 16:07:56] INFO  ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2]
== Sinatra/1.4.2 has taken the stage on 4567 for development with backup from WEBrick
[2013-04-19 16:07:56] INFO  WEBrick::HTTPServer#start: pid=63798 port=4567
^C

任何人都可以帮我解决可能出现的问题吗?

1 个答案:

答案 0 :(得分:6)

问题是你没有正确使用Sinatra modular style。您需要sinatra并继承自Sinatra::Application,而非要求sinatra/base并继承Sinatra::Base

这是怎么回事。您需要简单的sinatra,而sinatra/main则需要run!。此文件adds an at_exit handler that runs the built in server(除非您禁用它)。但是,您还在自己的代码中显式调用at_exit,因此服务器因您的调用而启动,然后当您退出时sinatra/base处理程序再次启动服务器。要求run!在退出时无法启动内置服务器,因此您只能自己显式调用require 'sinatra/base' # change here require 'rubygems' class TestServer < Sinatra::Base # and here set :port, 22340 get '/' do "Hello World" end run! if app_file == $0 end

{{1}}