【问题标题】:Sinatra Restarting Webrick Server after ctrl-cSinatra 在 ctrl-c 后重新启动 Webrick 服务器
【发布时间】:2013-04-19 20:17:42
【问题描述】:
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

谁能帮我解决这里可能出了什么问题?

【问题讨论】:

    标签: ruby sinatra webrick


    【解决方案1】:

    问题是您没有正确使用Sinatra modular style。与其要求 sinatra 并从 Sinatra::Application 继承,不如要求 sinatra/base 并从 Sinatra::Base 继承。

    正在发生的事情是这样的。您需要普通的sinatra,而后者又需要sinatra/main。这个文件adds an at_exit handler that runs the built in server(除非你禁用它)。但是,您还可以在自己的代码中显式调用run!,因此服务器会因您的调用而启动,然后当您退出时,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
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2012-05-20
      • 2013-04-23
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多