运行Sinatra应用程序的首选方法是什么?

时间:2012-08-21 05:54:48

标签: sinatra bundler jruby-rack trinidad-gem

编辑:我让应用程序使用TorqueBox,所以我猜这个问题与特立尼达有关。但是,我认为我对运行Sinatra应用程序的首选方式的猜测仍然有效。

我使用的是JRuby,Bundler和模块化风格。这是我的应用程序代码:

# myapp.rb
class MyApp < Sinatra::Base
  configure :development do
    register Sinatra::Reloader
  end

  set :server, 'trinidad'
  set :environment, :development
  set :logging, true
  #...
end

# config.ru
require 'bundler/setup'

Bundler.require(:default)

$: << File.dirname(__FILE__)

require 'myapp'

run MyApp

# Gemfile
source 'http://rubygems.org'

gem 'rack'
gem 'sinatra', :require => 'sinatra/base'
gem 'sinatra-contrib', :require => 'sinatra/reloader'
gem 'coffee-script'
gem 'neography'
gem 'gon-sinatra'
gem 'sass'
gem 'trinidad'

启动我的应用程序的正确方法(即什么样的命令)是什么?我正在使用Trinidad,因为我读到它是JRuby应用程序的好解决方案(我无法让TorqueBox工作)。目前我正在通过'trinidad config.ru'运行我的应用程序,但它会抛出以下类似的错误消息:

application error
org.jruby.rack.RackInitializationException: no such file to load -- bundler/setup
    from org/jruby/RubyKernel.java:991:in `require'
    from /home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/custom_require.rb:36:in `require'
    from /home/myhome/RubymineProjects/myapp/config.ru:1:in `(root)'
    from org/jruby/RubyBasicObject.java:1818:in `instance_eval'
    from jar:file:/home/myhome/.rvm/gems/jruby-1.7.0.preview1/gems/jruby-rack-1.1.9/lib/jruby-rack-1.1.9.jar!/vendor/rack-1.4.1/rack/builder.rb:51:in `initialize'
    from /home/myhome/RubymineProjects/myapp/config.ru:1:in `(root)'

    at org.jruby.rack.DefaultRackApplicationFactory$4.init(DefaultRackApplicationFactory.java:224)
    at org.jruby.rack.DefaultRackApplicationFactory.getApplication(DefaultRackApplicationFactory.java:58)
    at org.jruby.rack.PoolingRackApplicationFactory.createApplication(PoolingRackApplicationFactory.java:323)
    at org.jruby.rack.PoolingRackApplicationFactory.getApplication(PoolingRackApplicationFactory.java:171)
    at org.jruby.rack.DefaultRackDispatcher.getApplication(DefaultRackDispatcher.java:27)
    at org.jruby.rack.AbstractRackDispatcher.process(AbstractRackDispatcher.java:32)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:37)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- bundler/setup
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
    at Kernel.require(/home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/custom_require.rb:36)
    at RUBY.(root)(/home/myhome/RubymineProjects/myapp/config.ru:1)
    at org.jruby.RubyBasicObject.instance_eval(org/jruby/RubyBasicObject.java:1818)
    at RUBY.initialize(jar:file:/home/myhome/.rvm/gems/jruby-1.7.0.preview1/gems/jruby-rack-1.1.9/lib/jruby-rack-1.1.9.jar!/vendor/rack-1.4.1/rack/builder.rb:51)
    at RUBY.(root)(/home/myhome/RubymineProjects/myapp/config.ru:1)
ignoring null application

我正在使用Trinidad和Rack的1.4.1版本,JRuby的1.7.0.preview1和Sinatra的1.3.3。

如果我运行'bundle exec trinidad config.ru',我会收到以下错误:

application error
org.jruby.rack.RackInitializationException: Valid types are [:development, :runtime], 
    from /home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/dependency.rb:45:in `initialize'
    from /home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/specification.rb:821:in `add_dependency_with_type'
    from :1:in `'
    from :1:in `'
    from org/jruby/RubyKernel.java:1037:in `eval'
    from org/jruby/RubyArray.java:1611:in `each'
    from org/jruby/RubyArray.java:1611:in `each'
    from org/jruby/RubyEnumerable.java:649:in `find_all'
    from org/jruby/RubyKernel.java:991:in `require'
    from org/jruby/RubyKernel.java:991:in `require'
    from org/jruby/RubyKernel.java:991:in `require'

    at org.jruby.rack.DefaultRackApplicationFactory.createApplication(DefaultRackApplicationFactory.java:235)
    at org.jruby.rack.DefaultRackApplicationFactory.newApplication(DefaultRackApplicationFactory.java:49)
    at org.jruby.rack.DefaultRackApplicationFactory.getApplication(DefaultRackApplicationFactory.java:57)
    at org.jruby.rack.PoolingRackApplicationFactory.createApplication(PoolingRackApplicationFactory.java:323)
    at org.jruby.rack.PoolingRackApplicationFactory.getApplication(PoolingRackApplicationFactory.java:171)
    at org.jruby.rack.DefaultRackDispatcher.getApplication(DefaultRackDispatcher.java:27)
    at org.jruby.rack.AbstractRackDispatcher.process(AbstractRackDispatcher.java:32)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:37)
    at org.jruby.rack.AbstractServlet.service(AbstractServlet.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.jruby.exceptions.RaiseException: (ArgumentError) Valid types are [:development, :runtime], 
    at Gem::Dependency.initialize(/home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/dependency.rb:45)
    at Gem::Specification.add_dependency_with_type(/home/myhome/.rvm/rubies/jruby-1.7.0.preview1/lib/ruby/shared/rubygems/specification.rb:821)
    at RUBY.(:1)
    at RUBY.(:1)
    at org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1037)
    at org.jruby.RubyArray.each(org/jruby/RubyArray.java:1611)
    at org.jruby.RubyArray.each(org/jruby/RubyArray.java:1611)
    at org.jruby.RubyEnumerable.find_all(org/jruby/RubyEnumerable.java:649)
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
    at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:991)
ignoring null application

2 个答案:

答案 0 :(得分:1)

默认情况下不加载rubygems(至少使用jruby --1.8)因此你可以在 config.ru 之上添加require 'rubygems'作为JRuby(实际上是JRuby-Rack) )默认情况下,将您的应用程序加载到单独的“运行时”(一种新的ruby进程,但在同一JVM进程中)。 或者,您也可以使用rackup -s trinidad

运行

答案 1 :(得分:0)

就调用机架配置和应用程序的CLI命令而言,kares是正确的。如果你没有使用Torquebox,那么使用带有“-s trinidad”的“rackup”就可以了。

至于从机架配置中调用应用程序的Ruby命令,这就是我所做的..不确定这是否适用于您的情况,如果这是您正在寻找的,但无论如何都会有所帮助HTTP处理程序:

你有MyApp,它混合在Sinatra :: Base中。如果你修改该类只配置你的环境,并为每个页面加载运行“before do”和“after do”...但实际上并不处理和获取/发布调用;然后,您可以创建专门的类来处理整个应用程序中的某些路径。这使用Rack :: Builder的map()方法......

require 'myapp'
require 'mylanding'
require 'myauthentication'
require 'myautocomplete'
require 'mysearch'

#de run MyApp

map "/" do
    run MyLanding    #de Catch-all, which would have "get '/*' do" in it too.
end

map "/authentication/" do
    run MyAuthentication
end

map "/autocomplete/" do
    run MyAutocomplete
end

map "/search/" do
    run MySearch
end

然后在每个My *类中,执行以下操作:

class MyLanding < MyApp
  get "/*" do
    ..handle landing.. or see what's in the splat.
  end
end

class MyAuthentication < MyApp
  get "/sign/in" do
    ..present sign-in..
  end
  post "/sign/in" do
    ..handle sign-in..
  end
end

等...

您的子类对每个页面加载都有整体MyApp处理,但添加路由,例如/ authentication / sign / in由MyAuthentication的/ sign / in处理。这是一个很酷的方式,使用相同的基础:MyApp。