メモ
- ほとんど参考サイトの写経
- ワーカープロセスを2つ動かす
事前準備
Gemfile
source 'https://rubygems.org'
gem "unicorn"
gem "rack"
Rackアプリケーションの要件は以下とのこと。
- callというメソッドを持っていること
- callメソッドの引数としてWebサーバからのリクエストを受けること
- callメソッドは,次の要素を含むレスポンスを返すること
- ステータスコード
- レスポンスヘッダ(Hash)
- レスポンスボディ(Array)
ちなみに以下変数 env
の中身はリクエスト時に展開すると、以下のようにハッシュ構造でHTTPリクエストが渡される
{"REMOTE_ADDR"=>"121.87.8.214",
"REQUEST_METHOD"=>"GET",
"REQUEST_PATH"=>"/",
"PATH_INFO"=>"/",
"REQUEST_URI"=>"/",
"SERVER_PROTOCOL"=>"HTTP/1.1",
"HTTP_VERSION"=>"HTTP/1.1",
"HTTP_HOST"=>"18.182.112.198:8080",
sample.rb
class SimpleApp
def call(env)
[
200,
{ "Content-Type" => "text/html" },
["contents"],
]
end
end
config.ru
# coding: utf-8
require_relative 'simple_app.rb'
run SimpleApp.new
unicorn.conf
worker_processes 2
実行
% bundle install --path vendor/bundle
# config.ru は省略可能
# config.ru はアプリケーションのルートファイルとして読み込まれる
% bundle exec unicorn -c unicorn.conf config.ru
参考
第23回 Rackとは何か(1)Rackの生まれた背景:Ruby Freaks Lounge|gihyo.jp … 技術評論社 http://gihyo.jp/dev/serial/01/ruby/0023
unicorn: Rack HTTP server for fast clients and Unix https://yhbt.net/unicorn/README.html