Rack app的定义方式是:
?
class="ruby" name="code">require ‘rack’ class MyServer def call(env) [200, {}, ['hello world']] end end Rack::Handler::WEBrick.run MyServer.new, Port: 2000
?要给app加一些中间件,就需要这么写:
app = MyServer.new app = Rack::Lint.new(app) app = Rack::ShowStatus.new(app) app = Rack::ShowExceptions.new(app) app = Rack::CommonLogger.new(app)
Rack有自己的DSL(领域内专属语言)叫Rack::Builder,定义方法是:
?
?
app = Rack::Builder.new do use Rack::CommonLogger use Rack::ShowExceptions use Rack::ShowStatus use Rack::Lint run MyServer.new end
?也可以把DSL的代码写在单独的ru配置文件中,这种方式形成的文件叫rackup文件:
?
?
#!/usr/bin/env rackup use Rack::CommonLogger use Rack::ShowExceptions use Rack::ShowStatus use Rack::Lint run MyServer.new
?执行myserver.ru -s webrick -p 2000
?