
機架應用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 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。版權所有。