ほとんど参考サイトの再構成によるメモ
rackup コマンドを使う
- rackup コマンドで実行する
- コード内で
run [RackApp]
を実行する -
config.ru
に設定やコードを書く(ただし rackup コマンド実行時に別のファイルを指定も可能)
インスタンスメソッドを使う
実行コマンド
> bundle exec rackup -o 0.0.0.0
config.ru
require './app.rb'
run SimpleApp.new
app.rb
class SimpleApp
def call(env)
[200,{ 'Content-Type' => 'text/html' },["Hello"]]
end
end
もちろん以下のように config.ru
にまとめても可。
config.ru
class SimpleApp
def call(env)
[200,{ 'Content-Type' => 'text/plain' },["Hello"]]
end
end
run SimpleApp.new
クラスメソッドを使う
実行コマンド
> bundle exec rackup -o 0.0.0.0
config.ru
class SimpleApp
def self.call(env)
[200, {'Content-Type' => 'text/plain'}, ['Hello']]
end
end
run SimpleApp
Proc.new を使う
生成されるオブジェクトは call
メソッドを持つ。
実行コマンド
> bundle exec rackup -o 0.0.0.0
config.ru
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['Hello']] }
以下でもおk。
config.ru
run lambda {|env| [200, {'Content-Type' => 'text/plain'}, ['Hello']] }
rackup コマンドを使わない(ハンドラから直接実行する)
実行コマンド
> bundle exec ruby my_rack_app.rb
app.rb
require 'rack'
app = Proc.new do |env|
['200', {'Content-Type' => 'text/html'}, ['Hello']]
end
Rack::Handler::WEBrick.run app
参考サイト
Rack解説 - Rackの構造とRack DSL - Qiita https://qiita.com/higuma/items/838f4f58bc4a0645950a#rack%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E4%BB%95%E6%A7%98
Rack: a Ruby Webserver Interface http://rack.github.io/