Sinatra 应用程序未启动内部网络服务器

Sinatra app is not starting the internal webserver

我有一个非常简单的 Sinatra 应用程序要测试。

ubuntu@ip:~/helloworld$ cat app.rb
# app.rb
require 'sinatra'

class HelloWorldApp < Sinatra::Base
  get '/' do
    "Hello, world!"
  end
end
ubuntu@:~/helloworld$ cat config.ru
# config.ru
require './app'
run HelloWorldApp

当我在 Ubuntu 上启动它时,它是这样运行的。它没有启动监听器。 或者网络服务器

$ rackup
[2016-03-18 22:23:58] INFO  WEBrick 1.3.1
[2016-03-18 22:23:58] INFO  ruby 2.2.3 (2015-08-18) [x86_64-linux]
[2016-03-18 22:23:58] INFO  WEBrick::HTTPServer#start: pid=18049 port=9292

或者这样

$ ruby app.rb
[2016-03-18 22:28:00] INFO  WEBrick 1.3.1
[2016-03-18 22:28:00] INFO  ruby 2.2.3 (2015-08-18) [x86_64-linux]
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from WEBrick
[2016-03-18 22:28:00] INFO  WEBrick::HTTPServer#start: pid=18087 port=4567

在 Mac 上,当我启动一个应用程序时,它是这样运行的。

$ ruby app.rb
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop

它正在本地主机上侦听。我的 Ubuntu 无法启动内部网络服务器有什么问题。任何故障排除说明?

我认为这里发生了一些事情。

首先,Thin 与 Webrick。 Rack 和 Sinatra 都会在启动时尝试寻找合适的网络服务器。他们会寻找 Thin,但如果找不到,他们都会转而使用 Webrick。解决方案是使用 gem install thin 在 Ubuntu 服务器上安装 Thin。您可能需要考虑使用 Bundler 并向其中添加 thin gem 以确保您在开发和生产中始终拥有相同的 gem。

其次,从另一台机器访问服务器。默认情况下,在开发模式下启动时 rackup and Sinatra’s built in server will only listen to localhost. To bind to 0.0.0.0 you will either need to specify the host explicitly (either with the -o option to rackup or with set :bind '0.0.0.0' for the Sinatra built in sever), or start in production mode using the RACK_ENV environment variable.

另外一件事 – 在您当前的设置中 运行ning ruby app.rb 不会真正启动您的应用程序。它将 运行 默认 Sinatra::Application(在本例中为空)而不是您的 HelloWorldApp,因为您使用的是 modular style。要按预期将其设置为 运行,您应该将要求行更改为

require 'sinatra/base'

并添加

HelloWorldApp.run! if app_file == [=11=]

到文件末尾。