When you run a Sinatra app using the built-in web server (as you do with SinatraApp.run!
), Sinatra tries to determine which server to use by checking a list of servers in turn to see which is available. The actual list depends on the version of Ruby you are using, but one server that it always checks is net-http-server, which is simply named HTTP
.
The way Sinatra checks for the availability of a server is by using a rack method that calls const_get
to try and find the constant Rack::Handler::<server-name>
. However, due to the way const_get
works, if that constant is not available, but a top level constant with the same name as server-name
is, then that will be returned, whatever class it is. (This is arguably a bug in Rack).
The Twitter gem depends on the http
gem, and that in turn defines a HTTP
module. (Naming a top-level module with something as generic as HTTP
is arguably not a good idea).
So what is happening in this case is Sinatra is checking to see if the HTTP
server is available, but Rack is returning the HTTP
module from the http
gem, which isn’t a server. Not being a Rack server it doesn’t have a run
method, so when Sinatra tries to use it as one you get the error start_server': undefined method `run' for HTTP:Module
.
One workaround is not to use the built-in server, such as the way you have discovered using a config.ru
file and starting the app with rackup
.
Another solution is to explicitly specify the server to use in your Sinatra app. For example you could install Thin, and then use:
set :server, 'thin'
In fact simply installing Thin would be sufficient as Thin is searched for before HTTP, but you are probably better explicitly setting the server to use. If you cannot install any other server for any reason you could use Webrick instead:
set :server, 'webrick'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…