
机架应用GPT助手
rack-app是一个简约的Web框架,侧重于简单性和可维护性。该框架本来应由经验丰富的Web开发人员使用。
rack-app集中在尽可能少地保持依赖项,同时允许编写功能性和极简主义机架的应用程序,这将无助于您所定义的内容。
该路由使用前缀树,因此添加大量API端点不会影响路由查找时间。
它受到Sinatra , grape和rack的启发。它用于生产,为在公共云上运行的后端API提供动力。
该框架被认为是稳定的。我没有计划在没有现实生活用例的情况下将框架的特色特色,因为大多数自定义边缘案例都可以通过构图解决。
下次将收到进一步更新时,当机架提供了HTTP2的最终支持时。
如果您有问题,我每周检查“问题”选项卡,回答和答复或实施解决问题。
由于该框架的唯一依赖性是rack Gem,因此我不必经常更新代码库。
欢呼和愉快的编码!
将此行添加到您的应用程序的gemfile:
gem 'rack-app'然后执行:
$ bundle
或自行安装:
$ gem install rack-app
是的,它已经在为Heroku提供动力托管了微服务。
Rack-App的路由器依赖于大量使用常见前缀的树结构,它基本上是紧凑的前缀树(或仅是radix树)。带有常见前缀的节点也共享一个共同的父母。
丹尼尔·纳吉(Daniel Nagy)
丹尼尔·塞普斯吉克(Daniel Szpisjak)
杰里米·埃文斯(Jeremy Evans)
大卫·布什
Thesmartnik
require 'rack/app'
class App < Rack :: App
desc 'some hello endpoint'
get '/hello' do
'Hello World!'
end
end require 'rack/app'
class App < Rack :: App
mount SomeAppClass
headers 'Access-Control-Allow-Origin' => '*' ,
'Access-Control-Expose-Headers' => 'X-My-Custom-Header, X-Another-Custom-Header'
serializer do | object |
object . to_s
end
desc 'some hello endpoint'
validate_params do
required 'words' , :class => Array , :of => String , :desc => 'some word' , :example => [ 'pug' ]
optional 'word' , :class => String , :desc => 'one word' , :example => 'pug'
optional 'boolean' , :class => :boolean , :desc => 'boolean value' , :example => true
end
get '/hello' do
puts ( params [ 'words' ] )
'Hello World!'
end
namespace '/users' do
desc 'some restful endpoint'
get '/:user_id' do
response . status = 201
params [ 'user_id' ] #=> restful parameter :user_id
say #=> "hello world!"
end
end
desc 'some endpoint that has error and will be rescued'
get '/make_error' do
raise ( StandardError , 'error block rescued' )
end
def say
"hello #{ params [ 'user_id' ] } !"
end
error StandardError , NoMethodError do | ex |
{ :error => ex . message }
end
root '/hello'
get '/stream' do
stream do | out |
out << 'data row'
end
end
end您可以使用请求方法和Rack ::响应作为响应方法访问rack ::请求。
默认情况下,如果您不为响应“正文”写任何内容,则将使用端点块逻辑返回
如果您不介意扩展依赖项列表,则可以使用Front_end扩展名来创建基于模板的Web应用程序。
require 'rack/app'
require 'rack/app/front_end' # You need to add `gem 'rack-app-front_end'` to your Gemfile
class App < Rack :: App
apply_extensions :front_end
helpers do
def method_that_can_be_used_in_template
'hello world!'
end
end
# use ./app/layout.html.erb as layout, this is optionable
layout 'layout.html.erb'
# at '/' the endpoint will serve (render)
# the ./app/index.html content as response body and wrap around with layout if the layout is given
get '/' do
render 'index.html'
end
end此示例期望“ app.rb”文件旁边一个“应用”文件夹,其中包括所使用的模板,例如layout.html.erb和index.html。
用于测试使用机架/测试或捆绑的测试模块用于架架应用程序的编写单元测试
require 'spec_helper'
require 'rack/app/test'
describe App do
include Rack :: App :: Test
rack_app described_class
describe '/hello' do
# example for params and headers and payload use
subject { get ( url : '/hello' , params : { 'dog' => 'meat' } , headers : { 'X-Cat' => 'fur' } , payload : 'some string' ) }
it { expect ( subject . status ) . to eq 200 }
it { expect ( subject . body ) . to eq "Hello World!" }
end
describe '/users/:user_id' do
# restful endpoint example
subject { get ( url : '/users/1234' ) }
it { expect ( subject . body ) . to eq 'hello 1234!' }
it { expect ( subject . status ) . to eq 201 }
end
describe '/make_error' do
# error handled example
subject { get ( url : '/make_error' ) }
it { expect ( subject . body ) . to eq '{:error=>"error block rescued"}' }
end
end
官方网站如何示例
架子::应用团队GitHub存储库
基本的
埃舍尔授权API
检查回购后,运行bin/setup以安装依赖关系。然后,运行rake spec以运行测试。您还可以运行bin/console以获得交互提示,该提示可以让您进行实验。
要将此GEM安装到本地计算机上,请运行bundle exec rake install 。要发布新版本,请在version.rb中更新版本号,然后运行bundle exec rake release ,该版本将为版本创建一个git标签,推送git consits and tags,然后将.gem文件推到rubygems.org。
欢迎在https://github.com/rack-app/rack-app上的GitHub上的错误报告和拉动请求。此项目旨在是一个安全,热情的协作空间,预计贡献者有望遵守贡献者的契约行为守则。
RACK ::应用程序是根据Apache许可证V2许可证发布的免费软件。徽标是由ZsófiaGebauer设计的。它是版权©2015 Adam Luzsi。版权所有。